]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/mcsdk-tools.git/blob - writer/eeprom/src/eepromwriter.c
Added POST and EEPROM writer for C6670
[keystone-rtos/mcsdk-tools.git] / writer / eeprom / src / eepromwriter.c
1 /******************************************************************************
2  * Copyright (c) 2011 Texas Instruments Incorporated - http://www.ti.com
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    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
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *****************************************************************************/
34 /**************************************************************************************
35  * FILE PURPOSE: EEPROM writer utility
36  **************************************************************************************
37  * FILE NAME: eepromwriter.c
38  *
39  * DESCRIPTION: A simple EEPROM writer using platform lib APIs to program the EEPROM
40  *              with an image that the ibl can read.
41  *
42  ***************************************************************************************/
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include "platform.h"
47 #include "types.h"
49 /* EEPROM writer utility version */
50 char version[] = "01.00.00.01";
52 /* The input file name is hard coded */
53 char *input_file = "eepromwriter_input.txt";
55 /* Parameters defined in the input_file */
56 #define FILE_NAME      "file_name"
57 #define BUS_ADDR       "bus_addr"
58 #define START_ADDR     "start_addr"
60 /* Memory address to store the write data */
61 #define WRITE_DATA_ADDRESS     0x80000000
63 /******************************************************************************
64  * Structure:   EEPROM_WRITER_INFO_T
65  *
66  *              EEPROM writer control data. This structure should be filled in
67  *              by the user before running
68  ******************************************************************************/
69 #define MAX_LINE_LENGTH 40
70 typedef struct EEPROM_WRITER_INFO_tag
71 {
72     char        file_name[MAX_LINE_LENGTH]; /* CCS format data file name */
73     uint32_t    busAddr;                    /* Slave bus address */
74     uint32_t    startAddr;                  /* Start address to write */
75     uint32_t    deviceTotalBytes;           /* Total number of bytes available in the device */
76     uint32_t    writeBytes;                 /* Number of bytes to be written into the device */
77     uint8_t     *writeData;                 /* Address to store the write data */
78     uint8_t     *readData;                  /* Address to store the read data */
80 } EEPROM_WRITER_INFO_T;
82 EEPROM_WRITER_INFO_T eepromWriterInfo;
84 /******************************************************************************
85  * Function:    print_platform_errno
86  ******************************************************************************/
87 void
88 print_platform_errno
89 (
90     void
91 )
92 {
93     printf ("Returned platform error number is %d\n", platform_errno);
94 }
96 /******************************************************************************
97  * Function:    flash_eeprom
98  *
99  *              Write the data from memory to EEPROM.
100  *              Returns TRUE if the data is written successfully
101  *                      FALSE if the data write fails
102  ******************************************************************************/
103 Bool
104 flash_eeprom
106     PLATFORM_DEVICE_info    *p_device
109     printf ("Writing %d bytes from DSP memory address 0x%08x to EEPROM bus address 0x%04x starting from device address 0x%04x ...\n",
110             eepromWriterInfo.writeBytes,
111             (uint32_t)eepromWriterInfo.writeData,
112             eepromWriterInfo.busAddr,
113             eepromWriterInfo.startAddr);
115     if(platform_device_write(p_device->handle, eepromWriterInfo.startAddr, eepromWriterInfo.writeData, eepromWriterInfo.writeBytes) != Platform_EOK)
116     {
117         print_platform_errno();
118         return FALSE;
119     }
121     return TRUE;
124 /******************************************************************************
125  * Function:    flash_verify
126  *
127  *              Read back the data file that was just flashed.
128  *              Returns TRUE if the data verified correctly.
129  *                      FALSE if the data verification failed
130  ******************************************************************************/
131 Bool
132 flash_verify
134     PLATFORM_DEVICE_info    *p_device
137     uint32_t    i;
139     printf ("Reading %d bytes from EEPROM bus address 0x%04x to DSP memory address 0x%08x starting from device address 0x%04x ...\n",
140             eepromWriterInfo.writeBytes,
141             eepromWriterInfo.busAddr,
142             (uint32_t)eepromWriterInfo.readData,
143             eepromWriterInfo.startAddr);
145     if(platform_device_read(p_device->handle, eepromWriterInfo.startAddr, eepromWriterInfo.readData, eepromWriterInfo.writeBytes) != Platform_EOK)
146     {
147         print_platform_errno();
148         return FALSE;
149     }
151     printf ("Verifying data read ...\n");
153     for (i = 0; i < eepromWriterInfo.writeBytes; i++)
154     {
155         if (eepromWriterInfo.readData[i] != eepromWriterInfo.writeData[i])
156         {
157             printf ("Failure at byte %d, expected 0x%08x, read 0x%08x\n", i, eepromWriterInfo.writeData[i], eepromWriterInfo.readData[i]);
158             return (FALSE);
159         }
160     }
162     return (TRUE);
165 /******************************************************************************
166  * Function:    parse_input_file
167  ******************************************************************************/
168 static Bool
169 parse_input_file
171     FILE*               fp
174     char line[MAX_LINE_LENGTH];
175     char tokens[] = " :=;\n";
176     char *key, *data;
178     memset(line, 0, MAX_LINE_LENGTH);
180     fgets(line, MAX_LINE_LENGTH, fp);
181     key  = (char *)strtok(line, tokens);
182     data = (char *)strtok(NULL, tokens);
184     if(strlen(data) == 0)
185     {
186        return FALSE;
187     }
189     if(strcmp(key, FILE_NAME) != 0)
190     {
191         return FALSE;
192     }
194     strcpy (eepromWriterInfo.file_name, data);
196     fgets(line, MAX_LINE_LENGTH, fp);
197     key  = (char *)strtok(line, tokens);
198     data = (char *)strtok(NULL, tokens);
200     if(strlen(data) == 0)
201     {
202        return FALSE;
203     }
205     if(strcmp(key, BUS_ADDR) != 0)
206     {
207         return FALSE;
208     }
210     eepromWriterInfo.busAddr = (uint32_t)atoi(data);
212     fgets(line, MAX_LINE_LENGTH, fp);
213     key  = (char *)strtok(line, tokens);
214     data = (char *)strtok(NULL, tokens);
216     if(strlen(data) == 0)
217     {
218        return FALSE;
219     }
221     if(strcmp(key, START_ADDR) != 0)
222     {
223         return FALSE;
224     }
226     eepromWriterInfo.startAddr = (uint32_t)atoi(data);
228     return TRUE;
231 /******************************************************************************
232  * Function:    parse_ccs_file
233  ******************************************************************************/
234 static Bool
235 parse_ccs_file
237     FILE*               fp
240     char        line[MAX_LINE_LENGTH];
241     char        *pEnd;
242     uint32_t    data_len, write_addr;
244     memset(line, 0, MAX_LINE_LENGTH);
246     fgets(line, MAX_LINE_LENGTH, fp);
248     /* Read the write address from the CCS header */
249     strtoul (line,&pEnd,16);
250     strtoul (pEnd,&pEnd,16);
251     write_addr = strtoul (pEnd,&pEnd,16);
252     strtoul (pEnd,&pEnd,16);
254     /* Read the data length */
255     data_len = (strtoul (pEnd,NULL,16)) * 4;
256     if (data_len > (eepromWriterInfo.deviceTotalBytes - eepromWriterInfo.startAddr))
257     {
258         printf ("The data file is too big to fit into the device.\n");
259         return FALSE;
260     }
262     eepromWriterInfo.writeBytes = data_len;
263     if (write_addr != WRITE_DATA_ADDRESS)
264         write_addr = WRITE_DATA_ADDRESS;
265     eepromWriterInfo.writeData  = (uint8_t *)write_addr;
266     eepromWriterInfo.readData   = (uint8_t *)(write_addr + eepromWriterInfo.deviceTotalBytes);
268     return TRUE;
271 /******************************************************************************
272  * Function:    main
273  ******************************************************************************/
274 void main ()
276     FILE                    *fp;
277     platform_init_flags     init_flags;
278     platform_init_config    init_config;
279     PLATFORM_DEVICE_info    *p_device;
280     Bool                    ret;
282     printf("EEPROM Writer Utility Version %s\n\n", version);
284     fp = fopen(input_file, "r");
285     if (fp == NULL)
286     {
287         printf("Error in opening %s input file\n", input_file);
288         return;
289     }
291     ret = parse_input_file(fp);
292     fclose (fp);
294     if (ret == FALSE)
295     {
296         printf("Error in parsing %s input file\n", input_file);
297         return;
298     }
300     /* Initialize main Platform lib */
301     memset(&init_config, 0, sizeof(platform_init_config));
302     memset(&init_flags, 0x01, sizeof(platform_init_flags));
303     init_flags.phy = 0;
305     if (platform_init(&init_flags, &init_config) != Platform_EOK)
306     {
307         printf ("Platform init failed!\n");
308         print_platform_errno();
309         return;
310     }
312     p_device = platform_device_open(eepromWriterInfo.busAddr, 0);
313     if (p_device == NULL)
314     {
315         printf ("EEPROM device open failed!\n");
316         print_platform_errno();
317         return;
318     }
319     eepromWriterInfo.deviceTotalBytes = p_device->block_count * p_device->page_count * p_device->page_size;
321     /* Open and find the length of the data file */
322     fp = fopen (eepromWriterInfo.file_name, "rb");
323     if (fp == NULL)
324     {
325         printf ("Failed to open file %s\n", eepromWriterInfo.file_name);
326         print_platform_errno();
327         platform_device_close(p_device->handle);
328         return;
329     }
331     /* Parse the CCS format file */
332     ret = parse_ccs_file(fp);
333     fclose (fp);
334     if (ret == FALSE)
335     {
336         printf("Error in parsing CCS file %s\n", eepromWriterInfo.file_name);
337         platform_device_close(p_device->handle);
338         return;
339     }
341     /* Write the EEPROM */
342     if (flash_eeprom (p_device) == FALSE)
343     {
344         printf ("EEPROM write failed\n");
345         platform_device_close(p_device->handle);
346         return;
347     }
349     /* verify the flash */
350     if(flash_verify (p_device) == FALSE)
351     {
352         printf ("EEPROM read verify failed\n");
353         platform_device_close(p_device->handle);
354         return;
355     }
358     printf ("EEPROM programming completed successfully\n");
360     platform_device_close(p_device->handle);
362     return;