[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_2_eng / packages / ti / board / diag / common / AM437x / diag_mmc.c
1 /*
2 * Copyright (c) 2016, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
33 #include "diag_mmc.h"
35 /* MMCSD function table for MMCSD implementation */
36 FATFS_DrvFxnTable FATFS_drvFxnTable = {
37 MMCSD_close,
38 MMCSD_control,
39 MMCSD_init,
40 MMCSD_open,
41 MMCSD_write,
42 MMCSD_read
43 };
45 /* FATFS configuration structure */
46 FATFS_HwAttrs FATFS_initCfg[_VOLUMES] =
47 {
48 {
49 0U
50 },
51 {
52 1U
53 },
54 {
55 2U
56 },
57 {
58 3U
59 }
60 };
62 /* FATFS objects */
63 FATFS_Object FATFS_objects[_VOLUMES];
65 /* FATFS configuration structure */
66 const FATFS_Config FATFS_config[_VOLUMES + 1] = {
67 {
68 &FATFS_drvFxnTable,
69 &FATFS_objects[0],
70 &FATFS_initCfg[0]
71 },
73 {
74 &FATFS_drvFxnTable,
75 &FATFS_objects[1],
76 &FATFS_initCfg[1]
77 },
79 {
80 &FATFS_drvFxnTable,
81 &FATFS_objects[2],
82 &FATFS_initCfg[2]
83 },
85 {NULL, NULL, NULL},
87 {NULL, NULL, NULL}
88 };
90 char gTmpBuf[SBL_MMCSD_DATA_BUF_SIZE] __attribute__ ((aligned (32U)));
92 int32_t SBL_MulticoreImageParse(FIL *fp, uint8_t dummy, sblEntryPoint_t *sblEntry)
93 {
94 FRESULT fResult;
95 uint32_t bytesRead = 0U;
96 ti_header imageHdr;
97 uint8_t *pDestAddr;
98 int32_t retStat = E_PASS;
99 uint32_t *pEntryPoint = &sblEntry->entryPoint_MPU_CPU0;
100 UART_printf("Copying application image from MMC/SD card to RAM\r\n");
101 fResult = f_read(fp, (uint8_t *)&imageHdr, 8U,
102 (uint32_t *)&bytesRead);
104 if(FR_OK != fResult)
105 {
106 UART_printf("\r\n Error reading application file\r\n");
107 retStat = E_FAIL;
108 }
110 if(8U != bytesRead)
111 {
112 retStat = E_FAIL;
113 }
115 pDestAddr = (uint8_t *)imageHdr.load_addr;
116 *pEntryPoint = imageHdr.load_addr;
118 if(E_PASS == retStat)
119 {
120 /*
121 * Enter a loop to repeatedly read data from the file and display it, until
122 * the end of the file is reached.
123 */
124 do
125 {
126 /*
127 * Read a block of data from the file. Read as much as can fit in the
128 * temporary buffer, including a space for the trailing null.
129 */
130 fResult = f_read(fp, gTmpBuf, sizeof(gTmpBuf) - 1,
131 (uint32_t *)&bytesRead);
133 /*
134 * If there was an error reading, then print a newline and return the
135 * error to the user.
136 */
137 if(fResult != FR_OK)
138 {
139 UART_printf("\r\n Error reading application file\r\n");
140 retStat = E_FAIL;
141 }
143 if(bytesRead >= sizeof(gTmpBuf))
144 {
145 retStat = E_FAIL;
146 }
148 /* Read the last chunk of the file that was received. */
149 memcpy(pDestAddr, gTmpBuf, (sizeof(gTmpBuf) - 1));
150 pDestAddr += (sizeof(gTmpBuf) - 1);
151 /*
152 * Continue reading until less than the full number of bytes are read.
153 * That means the end of the buffer was reached.
154 */
155 }
156 while((bytesRead == sizeof(gTmpBuf) - 1) && (E_PASS == retStat));
157 }
159 /* Return success. */
160 return retStat;
161 }