1 //*****************************************************************************
2 //
3 //! @file am_hal_secure_ota.c
4 //!
5 //! @brief Functions for secure over-the-air.
6 //!
7 //! @addtogroup SecOTA3 Secure-OTA - Secure Over-the-Air Functionality
8 //! @ingroup apollo3_hal
9 //! @{
10 //
11 //*****************************************************************************
12
13 //*****************************************************************************
14 //
15 // Copyright (c) 2023, Ambiq Micro, Inc.
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 // POSSIBILITY OF SUCH DAMAGE.
43 //
44 // This is part of revision release_sdk_3_1_1-10cda4b5e0 of the AmbiqSuite Development Package.
45 //
46 //*****************************************************************************
47 #include <stdint.h>
48 #include <stdbool.h>
49 #include "am_mcu_apollo.h"
50
51 //
52 // Local defines
53 //
54 #define FLASH_INVALID 0xFFFFFFFF
55
56 //
57 //! Internal OTA state information
58 //
59 typedef struct
60 {
61 uint32_t flashSize;
62 uint32_t otaDescAddr;
63 uint32_t numOta;
64 } am_hal_secure_ota_state_t;
65
66 static am_hal_secure_ota_state_t gSOtaState;
67
68 //*****************************************************************************
69 // Erase a flash page
70 //*****************************************************************************
71 static void
erase_flash_page(uint32_t ui32ProgamKey,uint32_t ui32Addr)72 erase_flash_page(uint32_t ui32ProgamKey, uint32_t ui32Addr)
73 {
74 uint32_t ui32CurrentPage, ui32CurrentBlock;
75
76 //
77 // Figure out what page and block we're working on.
78 //
79 ui32CurrentPage = AM_HAL_FLASH_ADDR2PAGE(ui32Addr);
80 ui32CurrentBlock = AM_HAL_FLASH_ADDR2INST(ui32Addr);
81
82 //
83 // Start a critical section.
84 //
85 AM_CRITICAL_BEGIN
86 am_hal_flash_page_erase(ui32ProgamKey,
87 ui32CurrentBlock, ui32CurrentPage);
88 //
89 // Exit the critical section.
90 //
91 AM_CRITICAL_END
92 }
93
94 //*****************************************************************************
95 //
96 // @brief Initialize OTA state
97 //
98 // Initializes the OTA state. This should be called before doing any other operation
99 //
100 // @param ui32ProgamKey - The Flash programming key
101 // @param pOtaDesc should be start of a flash page designated for OTA Descriptor
102 //
103 // This call will erase the flash page, which will then be incrementally
104 // populated as OTA's are added. It will also initialize the OTAPOINTER to point
105 // to this descriptor, marking it as invalid at the same time
106 //
107 // @return Returns AM_HAL_STATUS_SUCCESS on success
108 //
109 //*****************************************************************************
am_hal_ota_init(uint32_t ui32ProgamKey,uint32_t * pOtaDesc)110 uint32_t am_hal_ota_init(uint32_t ui32ProgamKey, uint32_t *pOtaDesc)
111 {
112 am_hal_mcuctrl_device_t sDevice;
113 uint32_t otaDescAddr = (uint32_t)pOtaDesc;
114
115 //
116 // Get chip specific info
117 //
118 am_hal_mcuctrl_info_get(AM_HAL_MCUCTRL_INFO_DEVICEID, &sDevice);
119 gSOtaState.flashSize = sDevice.ui32FlashSize;
120
121 //
122 // Validate the flash page
123 //
124 if ((otaDescAddr >= gSOtaState.flashSize) ||
125 (otaDescAddr & (AM_HAL_FLASH_PAGE_SIZE - 1)))
126 {
127 return AM_HAL_STATUS_INVALID_ARG;
128 }
129
130 //
131 // Erase the page
132 //
133 erase_flash_page(ui32ProgamKey, otaDescAddr);
134
135 //
136 // Initialize the OTA Pointer
137 //
138 MCUCTRL->OTAPOINTER = otaDescAddr;
139 gSOtaState.numOta = 0;
140 gSOtaState.otaDescAddr = otaDescAddr;
141
142 return AM_HAL_STATUS_SUCCESS;
143 }
144
145 //*****************************************************************************
146 //
147 // @brief Add a new image for OTA
148 //
149 // Adds a new image to the OTA Descriptor.
150 //
151 // @param ui32ProgamKey - The Flash programming key
152 // @param imageMagic image magic# identifying type of image being added to OTA descr
153 // @param pImage should point to the start of new image to be added to descr
154 //
155 // This will program the next available entry in OTA descriptor. It will also set
156 // appropriate state in the OTA pointer register
157 //
158 // @return Returns AM_HAL_STATUS_SUCCESS on success
159 //
160 //*****************************************************************************
am_hal_ota_add(uint32_t ui32ProgamKey,uint8_t imageMagic,uint32_t * pImage)161 uint32_t am_hal_ota_add(uint32_t ui32ProgamKey, uint8_t imageMagic, uint32_t *pImage)
162 {
163 uint32_t imageAddr = (uint32_t)pImage;
164
165 //
166 // Validate the Image Pointer
167 //
168 if ((imageAddr >= gSOtaState.flashSize) ||
169 (imageAddr & (AM_HAL_FLASH_PAGE_SIZE - 1)))
170 {
171 return AM_HAL_STATUS_INVALID_ARG;
172 }
173
174 if (gSOtaState.numOta == AM_HAL_SECURE_OTA_MAX_OTA)
175 {
176 return AM_HAL_STATUS_OUT_OF_RANGE;
177 }
178
179 imageAddr |= AM_HAL_OTA_STATUS_PENDING;
180
181 //
182 // Program the OTA Descriptor word
183 //
184 am_hal_flash_program_main(ui32ProgamKey,
185 &imageAddr,
186 ((uint32_t *)gSOtaState.otaDescAddr + gSOtaState.numOta++),
187 1);
188
189 //
190 // Set appropriate OTA Pointer bits
191 //
192 MCUCTRL->OTAPOINTER_b.OTAVALID = 1;
193 if (imageMagic == AM_IMAGE_MAGIC_SBL)
194 {
195 MCUCTRL->OTAPOINTER_b.OTASBLUPDATE = 1;
196 }
197
198 return AM_HAL_STATUS_SUCCESS;
199 }
200
201 // Get OTA Status
202 //
203 //*****************************************************************************
204 //
205 // @brief Get Current OTA Descriptor state
206 //
207 // @details This will retrieve the current OTA status of various images added to the OTA descr
208 //
209 // @note Can be called anytime (generally after coming back from reset to check
210 // the status of OTA
211 // @par
212 // @note Will be also used by sbl_main to identify list of OTA's left for it
213 // (would show up as PENDING)
214 //
215 // @param pOtaDesc should be start of a flash page designated for OTA Descriptor
216 // @param maxOta Determines the size of the following buffer
217 // @param pStatus - Return Parameter - populated by this function indicating the OTA
218 // status of various OTA's
219 //
220 // @return Returns AM_HAL_STATUS_SUCCESS on success
221 //
222 //*****************************************************************************
am_hal_get_ota_status(uint32_t * pOtaDesc,uint32_t maxOta,am_hal_ota_status_t * pStatus)223 uint32_t am_hal_get_ota_status(uint32_t *pOtaDesc, uint32_t maxOta, am_hal_ota_status_t *pStatus)
224 {
225 uint32_t numOta = 0;
226
227 //
228 // Fill up the return structure
229 //
230 while (maxOta--)
231 {
232 if (pOtaDesc[numOta] == FLASH_INVALID)
233 {
234 pStatus[numOta].pImage = (uint32_t *)pOtaDesc[numOta];
235 break;
236 }
237 else
238 {
239 pStatus[numOta].pImage = (uint32_t *)(pOtaDesc[numOta] & ~0x3);
240 pStatus[numOta].status = (am_hal_ota_status_e)(pOtaDesc[numOta] & 0x3);
241 }
242 numOta++;
243 }
244 return AM_HAL_STATUS_SUCCESS;
245 }
246 //*****************************************************************************
247 //
248 // End Doxygen group.
249 //! @}
250 //
251 //*****************************************************************************
252