]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/ibl.git/blob - src/main/iblmain.c
8507b5f2e4297c93040ce0c2770f6c5ce156f089
[keystone-rtos/ibl.git] / src / main / iblmain.c
1 /*****************************************************************************************
2  * FILE PURPOSE: Perform the top level boot
3  *****************************************************************************************
4  * FILE NAME: iblmain.c
5  *
6  * DESCRIPTION: The top level boot examines the boot configuration and performs boot
7  *                              based on this configuration
8  *
9  * @file iblmain.c
10  *
11  * @brief
12  *   This file is used to launch a boot based on the boot configuration structure
13  *
14  *****************************************************************************************/
15 #include "ibl.h"
16 #include "iblloc.h"
17 #include "iblcfg.h"
18 #include "device.h"
19 #include "ethboot.h"
20 #include "bis.h"
21 #include "coffwrap.h"
22 #include "iblbtbl.h"
23 #include "iblblob.h"
24 #include "timer.h"
25 #include "i2c.h"
26 #include "ibl_elf.h"
27 #include <string.h>
30 /**
31  * @brief The ibl table is declared.
32  *
33  * @details
34  *   The ibl table is declared uninitialized by this ibl program. An external
35  *   initialization can be performed if the default operation of the ibl is
36  *   not desired.
37  */
38 #pragma DATA_SECTION(ibl, ".ibl_config_table")
39 ibl_t ibl;
42 /**
43  * @brief The ibl status table is declared.
44  *  
45  * @details
46  *   The ibl status table is declared. It is initialized at run time
47  *   in function main.
48  */
49 #pragma DATA_SECTION(iblStatus, ".ibl_status_table")
50 iblStatus_t iblStatus;
53 /* Eat printfs */
54 void mprintf(char *x, ...) { }
56 /**
57  * @b Description
58  * @n
59  *
60  *  Returns TRUE if the input priority is valid and enabled
61  */
62 BOOL iblPriorityIsValid (uint32 priority)
63 {
64     if ( (priority >= ibl_HIGHEST_PRIORITY)  &&
65          (priority <= ibl_LOWEST_PRIORITY)   )
67         return (TRUE);
70     return (FALSE);
72 }
74 /**
75  * @b Description
76  * @n
77  *
78  *  Returns TRUE if the mac address is 0
79  */
80 BOOL iblMacAddrIsZero (uint8 *maddr)
81 {
82     int32 i;
84     for (i = 0; i < 6; i++)
85         if (maddr[i] != 0)
86             return (FALSE);
88     return (TRUE);
90 }
93 /**
94  * @b Description
95  * @n
96  *
97  * The main function kicks off the boot. If it does not find the magic value in the
98  *     configuration array then default values are loaded. This default load
99  *     is done only once at the start of boot. 
100  *
101  * @retval
102  *  None
103  */
104 void main (void)
106     int32 i, j;
108     /* Initialize the status structure */
109     memset (&iblStatus, 0, sizeof(iblStatus_t));
110     iblStatus.iblMagic = ibl_MAGIC_VALUE;
113     /* Power up the timer */
114     devicePowerPeriph (TARGET_PWR_TIMER_0);
116     /* Initialize the system timer (software tracking of the hardware timer state) */
117     timer_init ();
121     /* Load the default configuration table from the i2c. The actual speed of the device
122      * isn't really known here, since it is part of the table, so a compile time
123      * value is used (the pll may have been configured during the initial load) */
124     hwI2Cinit (IBL_I2C_DEV_FREQ_MHZ,        /* The CPU frequency during I2C data load */
125                DEVICE_I2C_MODULE_DIVISOR,   /* The divide down of CPU that drives the i2c */
126                IBL_I2C_CLK_FREQ_KHZ,        /* The I2C data rate used during table load */
127                IBL_I2C_OWN_ADDR);           /* The address used by this device on the i2c bus */
129     if (hwI2cMasterRead (IBL_I2C_CFG_TABLE_DATA_ADDR,    /* The address on the eeprom of the table */
130                          sizeof(ibl_t),                  /* The number of bytes to read */
131                          (UINT8 *)&ibl,                  /* Where to store the bytes */
132                          IBL_I2C_CFG_EEPROM_BUS_ADDR,    /* The bus address of the eeprom */
133                          IBL_I2C_CFG_ADDR_DELAY)         /* The delay between sending the address and reading data */
135          != I2C_RET_OK)  {
137        /* There is no recovery if the load of the configuration table failed */
138        iblStatus.tableLoadFail = 0x11111111;
139        for (;;);
141     }
144     /* Load default mac addresses for ethernet boot if requested */
145     for (i = 0; i < ibl_N_ETH_PORTS; i++)  {
147         if ( (iblPriorityIsValid (ibl.ethConfig[i].ethPriority)       )   &&
148              (iblMacAddrIsZero   (ibl.ethConfig[i].ethInfo.hwAddress) )   )
150             deviceLoadDefaultEthAddress (ibl.ethConfig[i].ethInfo.hwAddress);
152     }
155     /* Pll configuration is device specific */
156     devicePllConfig ();
158     /* DDR configuration is device specific */
159     deviceDdrConfig ();
161     /* Try booting forever */
162     for (;;)  {
164         /* Start looping through the boot modes to find the one with the lowest priority
165          * value, and try to boot it. If a boot mode is not supported the function
166          * statement is simply defined to be a void statement */
167         for (i = ibl_HIGHEST_PRIORITY; i < ibl_LOWEST_PRIORITY; i++)  {
169             for (j = 0; j < ibl_N_ETH_PORTS; j++)  {
170                 if (ibl.ethConfig[j].ethPriority == i)
171                 iblEthBoot (j);
172             }
174             if (ibl.nandConfig.nandPriority == i)
175                 iblNandBoot ();
177             iblStatus.heartBeat += 1;
178         }
180     }
183 } /* main */
186     
187 /**
188  * @b Description
189  * @n
190  * 
191  * The ibl boot function links a device to a data format. The data format
192  * parser pulls data from the boot device
193  *
194  * @param[in] bootFxn      The structure containing the boot device functions
195  *
196  * @retval
197  *  None
198  */
199 Uint32 iblBoot (BOOT_MODULE_FXN_TABLE *bootFxn, Int32 dataFormat, void *formatParams)
200
201     Uint32 entry = 0;
203     union  {
205         Uint8   dataBuf[4];   /* Place holder */
206         Uint32  bisValue;
207         Uint16  coffVer;    
209     } fid;
212     /* Determine the data format if required */
213     if (dataFormat == ibl_BOOT_FORMAT_AUTO)  {
215         (*bootFxn->peek)((Uint8 *)&fid, sizeof(fid));
217         /* BIS */
218         if (fid.bisValue == BIS_MAGIC_NUMBER)
219             dataFormat = ibl_BOOT_FORMAT_BIS;
221         if (iblIsCoff (fid.coffVer))
222             dataFormat = ibl_BOOT_FORMAT_COFF;
224         if (iblIsElf (fid.dataBuf))
225             dataFormat = ibl_BOOT_FORMAT_ELF;
227         else  {
228             iblStatus.autoDetectFailCnt += 1;
229             return (0);
230         }
231     }        
234     /* Invoke the parser */
235     switch (dataFormat)  {
237         case ibl_BOOT_FORMAT_BIS:
238             iblBootBis (bootFxn, &entry);
239             break;
241         case ibl_BOOT_FORMAT_COFF:
242             iblBootCoff (bootFxn, &entry);
243             break;
245         case ibl_BOOT_FORMAT_BTBL:
246             iblBootBtbl (bootFxn, &entry);
247             break;
249         case ibl_BOOT_FORMAT_BBLOB:
250             iblBootBlob (bootFxn, &entry, formatParams);
251             break;
253         case ibl_BOOT_FORMAT_ELF:
254             iblBootElf (bootFxn, &entry);
255             break;
257         default:
258             iblStatus.invalidDataFormatSpec += 1;
259             break;
261     }
262     
264     return (entry);