1 /**
2 * \file sbl_mmcsd.c
3 *
4 * \brief This file contains functions for MMCSD File read operations for SBL
5 *
6 */
8 /*
9 * Copyright (C) 2015 - 2018 Texas Instruments Incorporated - http://www.ti.com/
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the
21 * distribution.
22 *
23 * Neither the name of Texas Instruments Incorporated nor the names of
24 * its contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 */
41 /* ========================================================================== */
42 /* Include Files */
43 /* ========================================================================== */
44 #include <stdint.h>
46 /* TI-RTOS Header files */
47 #include <ti/drv/uart/UART_stdio.h>
48 #include <ti/drv/mmcsd/MMCSD.h>
49 #include <ti/drv/mmcsd/soc/MMCSD_soc.h>
50 #include <ti/drv/mmcsd/src/MMCSD_osal.h>
52 /* SBL Header files. */
53 #include "sbl_rprc_parse.h"
54 #include "sbl_mmcsd.h"
56 /* K3 Header files */
57 #ifdef BUILD_MCU
58 #include <sbl_sci_client.h>
59 #endif
61 #ifdef SECURE_BOOT
62 #include "sbl_sec.h"
63 #endif
65 /**
66 * \brief SBL_FileRead function reads N bytes from SD card and
67 * advances the cursor.
68 *
69 * \param buff - Pointer to data buffer
70 * \param fileptr - Read head pointer
71 * \param size - Number of bytes to read
72 *
73 * \return Error code on file error
74 */
75 int32_t SBL_FileRead(void *buff,
76 void *fileptr,
77 uint32_t size);
79 /**
80 * \brief SBL_FileSeek function to move the read head by n bytes
81 *
82 * \param srcAddr - Read head pointer
83 * \param location - Move the read head pointer by n bytes
84 *
85 * \return none
86 */
87 void SBL_FileSeek(void *fileptr, uint32_t location);
91 int32_t SBL_loadMMCSDBootFile(FIL * fp);
93 /* ========================================================================== */
94 /* Global Variables */
95 /* ========================================================================== */
96 /* MMCSD function table for MMCSD implementation */
97 FATFS_DrvFxnTable FATFS_drvFxnTable = {
98 &MMCSD_close,
99 &MMCSD_control,
100 &MMCSD_init,
101 &MMCSD_open,
102 &MMCSD_write,
103 &MMCSD_read
104 };
106 /* FATFS configuration structure */
107 #if defined(BOOT_EMMC)
108 FATFS_HwAttrs FATFS_initCfg[_VOLUMES] =
109 {
110 {
111 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
112 0U
113 #else
114 1U
115 #endif
116 },
117 {
118 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
119 0U
120 #else
121 1U
122 #endif
123 },
124 {
125 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
126 0U
127 #else
128 1U
129 #endif
130 },
131 {
132 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
133 0U
134 #else
135 1U
136 #endif
137 }
138 };
139 #else
140 FATFS_HwAttrs FATFS_initCfg[_VOLUMES] =
141 {
142 {
143 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
144 1U
145 #else
146 0U
147 #endif
148 },
149 {
150 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
151 0U
152 #else
153 1U
154 #endif
155 },
156 {
157 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
158 0U
159 #else
160 1U
161 #endif
162 },
163 {
164 #if defined(iceK2G) || defined(am65xx_evm) || defined(am65xx_idk) || defined(j721e_evm)
165 0U
166 #else
167 1U
168 #endif
169 }
170 };
171 #endif
173 /* FATFS objects */
174 FATFS_Object FATFS_objects[_VOLUMES];
176 /* FATFS Handle */
177 FATFS_Handle sbl_fatfsHandle = NULL;
179 /* FATFS configuration structure */
180 const FATFS_Config FATFS_config[_VOLUMES + 1] = {
181 {
182 &FATFS_drvFxnTable,
183 &FATFS_objects[0],
184 &FATFS_initCfg[0]
185 },
187 {
188 &FATFS_drvFxnTable,
189 &FATFS_objects[1],
190 &FATFS_initCfg[1]
191 },
193 {
194 &FATFS_drvFxnTable,
195 &FATFS_objects[2],
196 &FATFS_initCfg[2]
197 },
199 {NULL, NULL, NULL},
201 {NULL, NULL, NULL}
202 };
204 #ifdef iceK2G
205 extern MMCSD_v1_HwAttrs MMCSDInitCfg[];
206 #endif
210 #ifdef SECURE_BOOT
211 extern SBL_incomingBootData_S sblInBootData;
212 #endif
214 #ifdef BUILD_MCU
215 int32_t SBL_ReadSysfwImage(void **pBuffer, uint32_t num_bytes)
216 {
217 int32_t retVal = CSL_PASS;
218 const TCHAR *fileName = "0:/sysfw.bin";
219 FIL fp = {0};
220 FRESULT fresult;
221 uint32_t bytes_read = 0;
222 void *sysfw_ptr = *pBuffer;
223 MMCSD_v2_HwAttrs hwAttrsConfig;
225 if(MMCSD_socGetInitCfg(FATFS_initCfg[0].drvInst,&hwAttrsConfig)!=0) {
226 UART_printf("\nUnable to get config.Exiting. TEST FAILED.\r\n");
227 retVal = E_FAIL;
228 }
230 hwAttrsConfig.enableInterrupt = ((uint32_t)(0U));
231 hwAttrsConfig.configSocIntrPath=NULL;
233 if(MMCSD_socSetInitCfg(FATFS_initCfg[0].drvInst,&hwAttrsConfig)!=0) {
234 UART_printf("\nUnable to set config.Exiting. TEST FAILED.\r\n");
235 retVal = E_FAIL;
236 }
238 /* Initialization of the driver. */
239 FATFS_init();
241 /* MMCSD FATFS initialization */
242 FATFS_open(0U, NULL, &sbl_fatfsHandle);
244 fresult = f_open(&fp, fileName, ((BYTE)FA_READ));
245 if (fresult != FR_OK)
246 {
247 UART_printf("\n SD Boot - sysfw File open fails \n");
248 retVal = E_FAIL;
249 }
250 else
251 {
252 fresult = f_read(&fp, sysfw_ptr, num_bytes, &bytes_read);
253 if (fresult != FR_OK)
254 {
255 UART_printf("\n SD Boot - sysfw read fails \n");
256 retVal = E_FAIL;
257 }
259 f_close(&fp);
260 }
262 FATFS_close(sbl_fatfsHandle);
263 sbl_fatfsHandle = NULL;
265 return retVal;
266 }
267 #endif
269 int32_t SBL_MMCBootImage(sblEntryPoint_t *pEntry)
270 {
271 int32_t retVal = E_PASS;
272 const TCHAR *fileName = "0:/app";
273 FIL fp = {0};
274 FRESULT fresult;
277 #ifdef SECURE_BOOT
278 uint32_t authenticated = 0;
279 uint32_t srcAddr = 0;
280 uint32_t imgOffset = 0;
281 #endif
283 #ifdef iceK2G
284 MMCSDInitCfg[1].cardType = MMCSD_CARD_SD;
285 #endif
287 /* Initialization of the driver. */
288 FATFS_init();
290 /* MMCSD FATFS initialization */
291 FATFS_open(0U, NULL, &sbl_fatfsHandle);
293 fresult = f_open(&fp, fileName, ((BYTE)FA_READ));
294 if (fresult != FR_OK)
295 {
296 UART_printf("\n SD Boot - File open fails \n");
297 retVal = E_FAIL;
298 }
299 else
300 {
302 #ifndef SECURE_BOOT
303 fp_readData = &SBL_FileRead;
304 fp_seek = &SBL_FileSeek;
306 retVal = SBL_MulticoreImageParse((void *) &fp, 0, pEntry);
307 #else
309 fp_readData = &SBL_MemRead;
310 fp_seek = &SBL_MemSeek;
313 /* handling secure boot image */
314 if (E_PASS == SBL_loadMMCSDBootFile(&fp))
315 {
316 /* successfully loading boot image */
317 /* authentiate it */
318 authenticated = SBL_authentication(sblInBootData.sbl_boot_buff);
319 if (authenticated == 0)
320 {
321 /* fails authentiation */
322 UART_printf("\n SD Boot - fail authentication\n");
324 retVal = E_FAIL;
325 }
326 else
327 {
328 /* need to skip the TOC headers */
329 imgOffset = ((uint32_t*)sblInBootData.sbl_boot_buff)[0];
330 srcAddr = (uint32_t)(sblInBootData.sbl_boot_buff) + imgOffset;
331 retVal = SBL_MulticoreImageParse((void *)srcAddr, 0, pEntry);
332 }
333 }
334 else
335 {
336 UART_printf("\n SD sec Boot - incorrect image\n");
338 retVal = E_FAIL;
339 }
341 #endif
343 f_close(&fp);
344 }
346 FATFS_close(sbl_fatfsHandle);
347 sbl_fatfsHandle = NULL;
349 #ifdef SECURE_BOOT
350 /* install RAM Secure Kernel to overwrite DSP secure server*/
351 UART_printf("\n Starting Secure Kernel on DSP...\n");
352 SBL_startSK();
353 #endif
355 return retVal;
356 }
358 #ifndef SECURE_BOOT
359 int32_t SBL_FileRead(void *buff,
360 void *fileptr,
361 uint32_t size)
362 {
363 FIL *fp = (FIL *) (fileptr);
364 uint8_t *tmp_buff_ptr = (uint8_t *)buff;
365 uint32_t i = 0;
366 uint32_t bytes_read = 0;
367 uint32_t Max_read = 0x400U; /*setting a fatfs read size of 1k */
368 FRESULT fresult = FR_OK;
369 int32_t retVal = E_FAIL;
371 for (i = ((uint32_t)0U); i < (size / Max_read); ++i)
372 {
373 fresult = f_read(fp, (void *)tmp_buff_ptr, Max_read, &bytes_read);
374 tmp_buff_ptr = tmp_buff_ptr + bytes_read;
375 if (fresult != FR_OK)
376 {
377 break;
378 }
379 }
380 if (fresult == FR_OK)
381 {
382 fresult = f_read(fp, (void *)tmp_buff_ptr, (UINT) (size % Max_read), &bytes_read);
383 }
385 if (fresult == FR_OK)
386 {
387 retVal = E_PASS;
388 }
390 return retVal;
391 }
393 void SBL_FileSeek(void *fileptr, uint32_t location)
394 {
395 FIL *fp = (FIL *) (fileptr);
396 f_lseek(fp, location);
397 }
399 #else
401 /* load signed boot image from MMCSD */
402 int32_t SBL_loadMMCSDBootFile(FIL * fp)
403 {
404 int32_t retVal = E_PASS;
405 uint32_t bytes_read;
406 uint32_t doneRead = 0;
407 uint32_t buff_idx = 0;
408 FRESULT fresult = FR_OK;
411 /* reading entire boot image into memory */
412 buff_idx = 0;
413 while ((doneRead == 0) &&
414 ((buff_idx + READ_BUFF_SIZE) < SBL_MAX_BOOT_BUFF_SIZE))
415 {
416 fresult = f_read(fp, (sblInBootData.sbl_boot_buff + buff_idx),
417 READ_BUFF_SIZE, &bytes_read);
419 if (fresult == FR_OK)
420 {
421 if (bytes_read < READ_BUFF_SIZE)
422 {
423 doneRead = 1;
424 }
426 buff_idx += bytes_read;
427 }
428 else
429 {
430 doneRead = 1;
432 /* fail read */
433 retVal = E_FAIL;
434 }
436 if ((doneRead == 0) &&
437 ((buff_idx + READ_BUFF_SIZE)>=SBL_MAX_BOOT_BUFF_SIZE))
438 {
439 /* boot image is bigger than reserved buffer. Error */
440 doneRead = 1;
441 retVal = E_FAIL;
442 }
443 }
445 sblInBootData.sbl_boot_size = buff_idx;
446 sblInBootData.sbl_boot_buff_idx = 0; /* reset the read pointer */
448 return retVal;
449 }
450 #endif