[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_1_eng / packages / ti / boot / sbl / src / mmcsd / sbl_mmcsd.c
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 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_v1.h>
50 #include <ti/drv/mmcsd/src/MMCSD_osal.h>
52 /* FATFS header file */
53 #include <ti/fs/fatfs/ff.h>
54 #include <ti/fs/fatfs/FATFS.h>
56 /* SBL Header files. */
57 #include "sbl_image_copy.h"
58 #include "sbl_rprc_parse.h"
60 /**
61 * \brief SBL_FileRead function reads N bytes from SD card and
62 * advances the cursor.
63 *
64 * \param buff - Pointer to data buffer
65 * \param fileptr - Read head pointer
66 * \param size - Number of bytes to read
67 *
68 * \return Error code on file error
69 */
70 int32_t SBL_FileRead(void *buff,
71 void *fileptr,
72 uint32_t size);
74 /**
75 * \brief SBL_FileSeek function to move the read head by n bytes
76 *
77 * \param srcAddr - Read head pointer
78 * \param location - Move the read head pointer by n bytes
79 *
80 * \return none
81 */
82 void SBL_FileSeek(void *fileptr, uint32_t location);
84 /* ========================================================================== */
85 /* Global Variables */
86 /* ========================================================================== */
87 /* MMCSD function table for MMCSD implementation */
88 FATFS_DrvFxnTable FATFS_drvFxnTable = {
89 MMCSD_close,
90 MMCSD_control,
91 MMCSD_init,
92 MMCSD_open,
93 MMCSD_write,
94 MMCSD_read
95 };
97 /* FATFS configuration structure */
98 FATFS_HwAttrs FATFS_initCfg[_VOLUMES] =
99 {
100 {
101 0U
102 },
103 {
104 1U
105 },
106 {
107 2U
108 },
109 {
110 3U
111 }
112 };
114 /* FATFS objects */
115 FATFS_Object FATFS_objects[_VOLUMES];
117 /* FATFS configuration structure */
118 const FATFS_Config FATFS_config[_VOLUMES + 1] = {
119 {
120 &FATFS_drvFxnTable,
121 &FATFS_objects[0],
122 &FATFS_initCfg[0]
123 },
125 {
126 &FATFS_drvFxnTable,
127 &FATFS_objects[1],
128 &FATFS_initCfg[1]
129 },
131 {
132 &FATFS_drvFxnTable,
133 &FATFS_objects[2],
134 &FATFS_initCfg[2]
135 },
137 {NULL, NULL, NULL},
139 {NULL, NULL, NULL}
140 };
142 int32_t SBL_MMCBootImage(sblEntryPoint_t *pEntry)
143 {
144 int32_t retVal;
145 char *fileName = "/app";
146 FIL fp;
147 FRESULT fresult;
148 FATFS_Handle fatfsHandle = NULL;
150 /* Initialization of the driver. */
151 fresult = FATFS_init();
153 /* MMCSD FATFS initialization */
154 fresult = FATFS_open(0U, NULL, fatfsHandle);
156 fresult = f_open(&fp, fileName, (uint8_t) FA_READ);
157 if (fresult != FR_OK)
158 {
159 UART_printf("\n SD Boot - File open fails \n");
160 retVal = E_FAIL;
161 }
162 else
163 {
164 UART_printf("\n SD Boot - file open completed successfully \n");
166 fp_readData = &SBL_FileRead;
167 fp_seek = &SBL_FileSeek;
169 retVal = SBL_MulticoreImageParse((void *) &fp, 0, pEntry);
171 f_close(&fp);
172 }
174 return retVal;
175 }
177 int32_t SBL_FileRead(void *buff,
178 void *fileptr,
179 uint32_t size)
180 {
181 FIL *fp = (FIL *) (fileptr);
182 uint32_t i = 0;
183 uint32_t bytes_read = 0;
184 uint32_t Max_read = 0x400U; /*setting a fatfs read size of 1k */
185 FRESULT fresult = FR_OK;
187 for (i = 0; i < (size / Max_read); ++i)
188 {
189 fresult = f_read(fp, buff, Max_read, &bytes_read);
190 buff += bytes_read;
191 if (fresult != FR_OK)
192 {
193 break;
194 }
195 }
196 if (fresult == FR_OK)
197 {
198 fresult = f_read(fp, buff, (uint16_t) (size % Max_read), &bytes_read);
199 }
201 return fresult;
202 }
204 void SBL_FileSeek(void *fileptr, uint32_t location)
205 {
206 FIL *fp = (FIL *) (fileptr);
207 f_lseek(fp, location);
208 }