CMSIS2000  0.0.7
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uboot_load_file.c
Go to the documentation of this file.
1 /*
2  * CMSIS2000
3  * CMSIS-like sources for LPC2xxx series MCUs
4  *
5  * (C) Copyright 2011-2012, Dmitriy Cherepanov, All Rights Reserved
6  *
7  * Version: 0.0.7
8  * Date of the Last Update: 2013-03-04
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to
12  * deal in the Software without restriction, including without limitation the
13  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14  * sell copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26  * IN THE SOFTWARE.
27  *
28  * Do not be lasy! For the latest version see http://irtos.sourceforge.net .
29  *
30  *
31 #*/
32 /*----------------------------------------------------------------------------*/
33 /**
34 \file program file from flash
35 \author Cherepanov Dmitiy
36 */
37 /*----------------------------------------------------------------------------*/
38 #include "ff.h"// FAT FS
39 
40 #include "lpc17xx.h"
41 #include "lpc17xx_iap.h"
42 
43 #include "uboot_load_file.h"
44 
45 #ifndef FLASH_PROG_AREA_START
46  #define FLASH_PROG_AREA_START 0x0000
47 #endif
48 
49 #ifndef BUFF_SIZE
50  #define BUFF_SIZE ((int)IAP_WRITE_1024)
51 #endif
52 
53 
54 #ifndef INTERNAL_SRAM_BUFF_ADRESS
55  #define INTERNAL_SRAM_BUFF_ADRESS (LPC_RAM_BASE+0x8000-BUFF_SIZE)
56 #endif
57 
58 /*----------------------------------------------------------------------------*/
59 #define NO_ALIGNED_ACCES -10001
60 /*----------------------------------------------------------------------------*/
61 static int check_size_and_header(FIL * fil, const unsigned char * buf);
62 
63 
64 /*----------------------------------------------------------------------------*/
65 #define sbuffer ((const void *) INTERNAL_SRAM_BUFF_ADRESS)
66 int load_uboot_file(FIL * fil, /*const unsigned char * sbuffer*/
67  flash_addr_t free_area_adress )
68 {
69 uint32_t dest_adress = FLASH_PROG_AREA_START;
70 int error,br;
71 uint32_t flash_prog_area_prev_sect,flash_prog_area_curr_sect =-1;
72 
73  error = check_size_and_header(fil,sbuffer);
74  if (error != 0) return error;
75 
76  error = ReadBootCodeVer(&error, &br);/* just do some initialization*/
77  if(error != CMD_SUCCESS) return error;
78 
79  /* Copy source to destination */
80  for (;;) {
81  flash_prog_area_prev_sect = flash_prog_area_curr_sect;
82  /*if (free_area_adress & (BUFF_SIZE-1))
83  return NO_ALIGNED_ACCES;*/
84  /* search next free sector */
85  flash_prog_area_curr_sect = GetSecNum (free_area_adress - 1) + 1;
86 
87  free_area_adress = GetSecAddr(flash_prog_area_curr_sect);
88  if (flash_prog_area_prev_sect != flash_prog_area_curr_sect ) {
89  error = EraseSector(flash_prog_area_curr_sect,flash_prog_area_curr_sect);
90  if (error != CMD_SUCCESS) break; /* error */
91  error = BlankCheckSector(flash_prog_area_curr_sect,
92  flash_prog_area_curr_sect, NULL, NULL);
93  if(error != CMD_SUCCESS) break; /* error */
94  }
95  error = CopyRAM2Flash((void *)dest_adress, sbuffer,IAP_WRITE_1024);
96  if (error != CMD_SUCCESS) break; /* error */
97  error = Compare((void *)dest_adress, sbuffer,IAP_WRITE_1024);
98  dest_adress +=br;
99  if (error != CMD_SUCCESS) break; /* error */
100 
101  error = f_read(fil, sbuffer,BUFF_SIZE, &br); /* Read a chunk of src file */
102  if (error || br == 0) break; /* error or eof */
103  }
104 return error;
105 };
106 
107 /*----------------------------------------------------------------------------*/
108 #include "uboot_image_format_adapted.h" /*---UBOOT HEADER ----------*/
109 
110 static int check_size_and_header(FIL * fil, const unsigned char * buf)
111 {
112 int error,br;
113  //ASSERT(BUFF_SIZE < sizeof(struct image_header));
114  error = f_read(fil, buf,BUFF_SIZE, &br); /* Read a chunk of src file */
115  if (error != 0) return error;
117 }
118