1 /*
2 *
3 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
38 /************************************************************************************
39 * FILE PURPOSE: Wrapper for the TI boot table processor
40 ************************************************************************************
41 * FILE NAME: btblwrap.c
42 *
43 * DESCRIPTION: The IBL wrapper for the boot table processor.
44 *
45 ************************************************************************************/
46 #include "types.h"
47 #include "ibl.h"
48 #include "iblloc.h"
49 #include "btblwrap.h"
50 #include "btbl.h"
51 #include "btblloc.h"
52 #include "iblbtbl.h"
53 #include <stdlib.h>
54 #include <string.h>
57 /* Globals used by the TI boot table processor */
59 BOOT_TBL_CB_T tiBootTable;
60 bootStats_t bootStats;
61 Uint32 btblEcode;
62 Int32 btblWrapEcode;
65 #define MIN(a,b) ((a) < (b)) ? (a) : (b)
68 /************************************************************************************
69 * FUNCTION PURPOSE: The main boot wrapper for the TI boot table processor
70 ************************************************************************************
71 * DESCRIPTION: Interfaces the IBL to the TI boot table driver.
72 * The TI boot table driver operates in a push data mode, the
73 * IBL operates in a pull data mode. This function pulls data
74 * from the interface and pushes is to the TI driver. To do this
75 * the internal state of the TI boot table driver is used
76 * to determine how much data to pull.
77 ************************************************************************************/
78 void iblBootBtbl (BOOT_MODULE_FXN_TABLE *bootFxn, Uint32 *entry_point)
79 {
80 Uint32 readSize;
81 Uint32 blockSize;
82 Uint32 blockSize16;
83 Uint8 *data;
84 Uint16 *data16;
85 Uint16 t16;
87 Int32 i, j;
89 *entry_point = 0;
90 btblEcode = 0;
91 btblWrapEcode = 0;
93 /* Initialize stats and btbl state */
94 iblMemset (&bootStats, 0, sizeof(bootStats_t));
96 boot_init_boot_tbl_inst (&tiBootTable);
98 data = iblMalloc (TI_BTBL_BLOCK_SIZE);
99 if (data == NULL) {
100 btblWrapEcode = BTBL_WRAP_ECODE_MALLOC_FAIL;
101 return;
102 }
104 data16 = (Uint16 *)data;
106 while ((tiBootTable.state != BOOT_TBL_STATE_FLUSH) && (btblEcode == 0)) {
108 switch (tiBootTable.state) {
110 case BOOT_TBL_STATE_PAD: readSize = 2;
111 break;
113 case BOOT_TBL_STATE_INIT:
114 case BOOT_TBL_STATE_SIZE:
115 case BOOT_TBL_STATE_ADDR: readSize = 4;
116 break;
118 case BOOT_TBL_STATE_DATA: readSize = tiBootTable.section_size_bytes;
119 break;
120 }
123 while (readSize > 0) {
125 blockSize = MIN(readSize, TI_BTBL_BLOCK_SIZE);
126 blockSize16 = (blockSize + 1) >> 1;
128 /* No recovery on block read failure */
129 if ((*bootFxn->read)(data, blockSize) < 0) {
130 btblWrapEcode = BTBL_WRAP_ECODE_READ_FAIL;
131 iblFree (data);
132 return;
133 }
135 /* The data has been read as a byte stream. This data must
136 * be re-formatted as a 16 bit word stream */
137 for (j = i = 0; j < blockSize16; j++, i += 2) {
139 t16 = (data[i+0] << 8) | data[i+1];
140 data16[j] = t16;
142 }
145 /* Process the block */
146 boot_proc_boot_tbl (&tiBootTable, data16, blockSize16);
148 readSize = readSize - blockSize;
150 }
152 }
154 if (btblEcode == 0)
155 *entry_point = tiBootTable.code_start_addr;
157 iblFree (data);
159 }
161 /***********************************************************************************
162 * FUNCTION PURPOSE: Handle a fatal error from the boot table processor
163 ***********************************************************************************
164 * DESCRIPTION: The error code is stored. Processing is terminated
165 * through the global ecode value.
166 ***********************************************************************************/
167 void btblBootException (UINT32 ecode)
168 {
169 btblEcode = ecode;
170 btblWrapEcode = BTBL_WRAP_ECODE_BTBL_FAIL;
171 }