1 /*
2  * Copyright (c) 2021, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stdbool.h>
9 #include "flash_map/flash_map.h"
10 #include "target.h"
11 #include "Driver_Flash.h"
12 
13 /* When undefined FLASH_DEV_NAME_0 or FLASH_DEVICE_ID_0 , default */
14 #if !defined(FLASH_DEV_NAME_0) || !defined(FLASH_DEVICE_ID_0)
15 #define FLASH_DEV_NAME_0  FLASH_DEV_NAME
16 #define FLASH_DEVICE_ID_0 FLASH_DEVICE_ID
17 #endif
18 
19 /* When undefined FLASH_DEV_NAME_1 or FLASH_DEVICE_ID_1 , default */
20 #if !defined(FLASH_DEV_NAME_1) || !defined(FLASH_DEVICE_ID_1)
21 #define FLASH_DEV_NAME_1  FLASH_DEV_NAME
22 #define FLASH_DEVICE_ID_1 FLASH_DEVICE_ID
23 #endif
24 
25 /* When undefined FLASH_DEV_NAME_2 or FLASH_DEVICE_ID_2 , default */
26 #if !defined(FLASH_DEV_NAME_2) || !defined(FLASH_DEVICE_ID_2)
27 #define FLASH_DEV_NAME_2  FLASH_DEV_NAME
28 #define FLASH_DEVICE_ID_2 FLASH_DEVICE_ID
29 #endif
30 
31 /* When undefined FLASH_DEV_NAME_3 or FLASH_DEVICE_ID_3 , default */
32 #if !defined(FLASH_DEV_NAME_3) || !defined(FLASH_DEVICE_ID_3)
33 #define FLASH_DEV_NAME_3  FLASH_DEV_NAME
34 #define FLASH_DEVICE_ID_3 FLASH_DEVICE_ID
35 #endif
36 
37 #if defined(MCUBOOT_SWAP_USING_SCRATCH)
38 /* When undefined FLASH_DEV_NAME_SCRATCH or FLASH_DEVICE_ID_SCRATCH , default */
39 #if !defined(FLASH_DEV_NAME_SCRATCH) || !defined(FLASH_DEVICE_ID_SCRATCH)
40 #define FLASH_DEV_NAME_SCRATCH  FLASH_DEV_NAME
41 #define FLASH_DEVICE_ID_SCRATCH FLASH_DEVICE_ID
42 #endif
43 #endif  /* defined(MCUBOOT_SWAP_USING_SCRATCH) */
44 
45 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof((arr)[0]))
46 
47 /* Flash device names must be specified by target */
48 extern ARM_DRIVER_FLASH FLASH_DEV_NAME_0;
49 extern ARM_DRIVER_FLASH FLASH_DEV_NAME_1;
50 extern ARM_DRIVER_FLASH FLASH_DEV_NAME_2;
51 extern ARM_DRIVER_FLASH FLASH_DEV_NAME_3;
52 extern ARM_DRIVER_FLASH FLASH_DEV_NAME_SCRATCH;
53 
54 #if !defined(FLASH_DRIVER_LIST)
55 /* Default Drivers list */
56 const ARM_DRIVER_FLASH *flash_driver[] = {
57     &FLASH_DEV_NAME,
58 #if FLASH_DEV_NAME_0 != FLASH_DEV_NAME
59     &FLASH_DEV_NAME_0,
60 #endif
61 #if FLASH_DEV_NAME_1 != FLASH_DEV_NAME
62     &FLASH_DEV_NAME_1,
63 #endif
64 #if FLASH_DEV_NAME_2 != FLASH_DEV_NAME
65     &FLASH_DEV_NAME_2,
66 #endif
67 #if (MCUBOOT_IMAGE_NUMBER == 2)
68 #if FLASH_DEV_NAME_1 != FLASH_DEV_NAME
69     &FLASH_DEV_NAME_1,
70 #endif
71 #if FLASH_DEV_NAME_3 != FLASH_DEV_NAME
72     &FLASH_DEV_NAME_3,
73 #endif
74 #endif /* (MCUBOOT_IMAGE_NUMBER == 2) */
75 #if defined(MCUBOOT_SWAP_USING_SCRATCH) && \
76     defined(FLASH_DEV_NAME_SCRATCH) && FLASH_DEV_NAME_SCRATCH != FLASH_DEV_NAME
77     &FLASH_DEV_NAME_SCRATCH
78 #endif
79 };
80 #else
81 /* Platform driver list */
82 const ARM_DRIVER_FLASH *flash_driver[] = FLASH_DRIVER_LIST;
83 #endif /* !defined(FLASH_DRIVER_LIST) */
84 const int flash_driver_entry_num = ARRAY_SIZE(flash_driver);
85 
86 const struct flash_area flash_map[] = {
87     {
88         .fa_id = FLASH_AREA_0_ID,
89         .fa_device_id = FLASH_DEVICE_ID_0,
90         .fa_driver = &FLASH_DEV_NAME_0,
91         .fa_off = FLASH_AREA_0_OFFSET,
92         .fa_size = FLASH_AREA_0_SIZE,
93     },
94     {
95         .fa_id = FLASH_AREA_2_ID,
96         .fa_device_id = FLASH_DEVICE_ID_2,
97         .fa_driver = &FLASH_DEV_NAME_2,
98         .fa_off = FLASH_AREA_2_OFFSET,
99         .fa_size = FLASH_AREA_2_SIZE,
100     },
101 #if (MCUBOOT_IMAGE_NUMBER == 2)
102     {
103         .fa_id = FLASH_AREA_1_ID,
104         .fa_device_id = FLASH_DEVICE_ID_1,
105         .fa_driver = &FLASH_DEV_NAME_1,
106         .fa_off = FLASH_AREA_1_OFFSET,
107         .fa_size = FLASH_AREA_1_SIZE,
108     },
109     {
110         .fa_id = FLASH_AREA_3_ID,
111         .fa_device_id = FLASH_DEVICE_ID_3,
112         .fa_driver = &FLASH_DEV_NAME_3,
113         .fa_off = FLASH_AREA_3_OFFSET,
114         .fa_size = FLASH_AREA_3_SIZE,
115     },
116 #endif
117 #if defined(MCUBOOT_SWAP_USING_SCRATCH)
118     {
119         .fa_id = FLASH_AREA_SCRATCH_ID,
120         .fa_device_id = FLASH_DEVICE_ID_SCRATCH,
121         .fa_driver = &FLASH_DEV_NAME_SCRATCH,
122         .fa_off = FLASH_AREA_SCRATCH_OFFSET,
123         .fa_size = FLASH_AREA_SCRATCH_SIZE,
124     },
125 #endif /* defined(MCUBOOT_SWAP_USING_SCRATCH) */
126 };
127 
128 const int flash_map_entry_num = ARRAY_SIZE(flash_map);
129