1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <assert.h>
18 #include <zephyr/kernel.h>
19 #include "bootutil/image.h"
20 #include "bootutil/bootutil.h"
21 #include "bootutil/fault_injection_hardening.h"
22 #include "flash_map_backend/flash_map_backend.h"
23 
24 /* @retval 0: header was read/populated
25  *         FIH_FAILURE: image is invalid,
26  *         BOOT_HOOK_REGULAR if hook not implemented for the image-slot,
27  *         othervise an error-code value.
28  */
boot_read_image_header_hook(int img_index,int slot,struct image_header * img_hed)29 int boot_read_image_header_hook(int img_index, int slot,
30                                 struct image_header *img_hed)
31 {
32     if (img_index == 1 && slot == 0) {
33             img_hed->ih_magic = IMAGE_MAGIC;
34             return 0;
35     }
36 
37     return BOOT_HOOK_REGULAR;
38 }
39 
40 /* @retval FIH_SUCCESS: image is valid,
41  *         FIH_FAILURE: image is invalid,
42  *         fih encoded BOOT_HOOK_REGULAR if hook not implemented for
43  *         the image-slot.
44  */
boot_image_check_hook(int img_index,int slot)45 fih_ret boot_image_check_hook(int img_index, int slot)
46 {
47     if (img_index == 1 && slot == 0) {
48         FIH_RET(FIH_SUCCESS);
49     }
50 
51     FIH_RET(FIH_BOOT_HOOK_REGULAR);
52 }
53 
boot_perform_update_hook(int img_index,struct image_header * img_head,const struct flash_area * area)54 int boot_perform_update_hook(int img_index, struct image_header *img_head,
55                              const struct flash_area *area)
56 {
57     if (img_index == 1) {
58         return 0;
59     }
60 
61     return BOOT_HOOK_REGULAR;
62 }
63 
boot_read_swap_state_primary_slot_hook(int image_index,struct boot_swap_state * state)64 int boot_read_swap_state_primary_slot_hook(int image_index,
65                                            struct boot_swap_state *state)
66 {
67     if (image_index == 1) {
68         state->magic = BOOT_MAGIC_UNSET;
69         state->swap_type = BOOT_SWAP_TYPE_NONE;
70         state->image_num = image_index ; // ?
71         state->copy_done = BOOT_FLAG_UNSET;
72         state->image_ok = BOOT_FLAG_UNSET;
73 
74         return 0;
75     }
76 
77     return BOOT_HOOK_REGULAR;
78 }
79 
boot_copy_region_post_hook(int img_index,const struct flash_area * area,size_t size)80 int boot_copy_region_post_hook(int img_index, const struct flash_area *area,
81                                size_t size)
82 {
83     return 0;
84 }
85 
boot_serial_uploaded_hook(int img_index,const struct flash_area * area,size_t size)86 int boot_serial_uploaded_hook(int img_index, const struct flash_area *area,
87                                size_t size)
88 {
89     return 0;
90 }
91 
boot_img_install_stat_hook(int image_index,int slot,int * img_install_stat)92 int boot_img_install_stat_hook(int image_index, int slot, int *img_install_stat)
93 {
94     return BOOT_HOOK_REGULAR;
95 }
96