CMSIS2000  0.0.7
 Указатель Структуры данных Файлы Функции Переменные Определения типов Перечисления Элементы перечислений Макросы Группы Страницы
uboot_file.c
См. документацию.
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 find and load file in memory
35 \author Cherepanov Dmitiy
36 */
37 /*----------------------------------------------------------------------------*/
38 #include "uboot_file.h"
39 #include <string.h>
40 /*----------------------------------------------------------------------------*/
41 #define IMAGE_STEP 0x100
42 
43 /*----------------------------------------------------------------------------*/
44 
45 static flash_addr_t uboot_check_image_not_aligned (flash_addr_t search_start_address,
46  flash_addr_t search_end_address);
47 /*----------------------------------------------------------------------------*/
49  flash_addr_t search_end_address)
50 {
51 flash_addr_t i = search_start_address;
52 
53  if (uboot_check_image_at_addr((const void * const)i,HAS_FULL_IMAGE))
54  return i;
55 
56  for (i= (i + (IMAGE_STEP - 1))&(~(IMAGE_STEP - 1));
57  i < search_end_address;i+=IMAGE_STEP) {
58  if (uboot_check_image_at_addr((const void * const)i,
60  return i;
61  };
62 
63  i = uboot_check_image_not_aligned(search_start_address,search_end_address);
64  if (i != 0)
65  return i;
66  else
67  return( (flash_addr_t )(-1));
68 };
69 
70 #include "uboot_image_format_adapted.h" /*---UBOOT HEADER ----------*/
71 
72 #ifdef UBOOT_APPLIC_NAME_START
73 // #define UBOOT_APPLIC_NAME_START "linu"
74 const char uboot_applic_name_str[] = UBOOT_APPLIC_NAME_START;
75 #endif // UBOOT_APPLIC_NAME_START
76 /*----------------------------------------------------------------------------*/
77 unsigned int uboot_check_image_at_addr (const void * const addr,char only_header)
78 {
79  const struct image_header * const img_hdr = (const struct image_header * const )addr;
80 
81  if ( image_check_magic(img_hdr) &&
82  image_check_hcrc (img_hdr) &&
84 /*
85  image_check_os(img_hdr,IH_OS_INVALID) &&
86  image_check_type(img_hdr,IH_TYPE_FIRMWARE) &&
87 */
88  ) {
89 #ifdef UBOOT_APPLIC_NAME_START
90  if (memcmp (image_get_name(img_hdr),uboot_applic_name_str) == 0)
91 #endif
92  if (only_header) {
93  return 1;
94  } else {
95  //if (image_check_dcrc(img_hdr))
96  return 1;
97  }
98  }
99 return 0;
100 }
101 /*----------------------------------------------------------------------------
102 flash_addr_t uboot_file_load_and_check_crc (flash_addr_t src_address, uint32_t no_need_crc )
103 {
104 struct image_header * img = (struct image_header * )src_address;
105 
106  if (image_get_comp(img) == IH_COMP_NONE) {
107  if (no_need_crc != 0) {
108  memcpy((void *) image_get_load(img),
109  (void *) image_get_data(img),
110  image_get_data_size(img));
111  } else {
112  no_need_crc = crc32_and_load((void *) image_get_data(img),
113  image_get_data_size(img),(void *) image_get_load(img));
114  if (no_need_crc != image_get_dcrc(img))
115  return ((flash_addr_t )(-1));
116  }
117 
118  #if defined(USE_COMP)
119  } else if (image_get_comp(img) == IH_COMP_LZO){
120  decompress ((void *) image_get_load(img),
121  (void *) image_get_data(img),
122  image_get_data_size(img));
123  #endif
124  } else {
125  return( (flash_addr_t )(-1));
126  }
127 return image_get_ep(img);
128 };*/
129 
130 /*----------------------------------------------------------------------------*/
131 static inline void * round_down_pointer(void * ptr)
132 {
133 unsigned int p = (unsigned int ) ptr;
134  return ( void * )(p & (~(0x3)));
135 }
137 {
138 struct image_header * img = (struct image_header * )src_address;
139 void * dst = image_get_load(img);
140 size_t size = image_get_data_size(img);
141 
142  if (image_get_comp(img) == IH_COMP_NONE) {
143  /* fill zero to the end of RAM data */
144  *(unsigned int *)round_down_pointer(dst+size) = 0;
145  memcpy (dst, (void *) image_get_data(img), size);
146  #if defined(USE_COMP)
147  } else if (image_get_comp(img) == IH_COMP_LZO){
148  decompress (dst, (void *) image_get_data(img), size);
149  #endif
150  } else {
151  return( (flash_addr_t )(-1));
152  }
153 return image_get_ep(img);
154 };
155 /*----------------------------------------------------------------------------*/
157 {
158  return image_get_load((struct image_header * )src_address);
159 }
160 /*----------------------------------------------------------------------------*/
161 /*----------------------------------------------------------------------------*/
162 #include "Utils/CRC/crc32.h"
163 
164 int image_check_hcrc(const image_header_t * const hdr)
165 {
166  size_t len = image_get_header_size();
167 
168  uint32_t hcrc = 0xa8dc0d46; /*let free compiler from calculating const value*/
169  /*uint32_t hcrc = crc32_init();
170  hcrc = crc32_update(hcrc, (uint8_t)(IH_MAGIC>>24) & 0xFF);
171  hcrc = crc32_update(hcrc, (uint8_t)(IH_MAGIC>>16) & 0xFF);
172  hcrc = crc32_update(hcrc, (uint8_t)(IH_MAGIC>>8) & 0xFF);
173  hcrc = crc32_update(hcrc, (uint8_t)(IH_MAGIC>>0) & 0xFF);
174  hcrc = crc32_update(hcrc, (uint8_t)0);
175  hcrc = crc32_update(hcrc, (uint8_t)0);
176  hcrc = crc32_update(hcrc, (uint8_t)0);
177  hcrc = crc32_update(hcrc, (uint8_t)0); */
178 
179  hcrc = crc32_no_comp(hcrc,
180  ((unsigned char *)hdr) + sizeof(hdr->ih_magic) + sizeof(hdr->ih_hcrc),
181  len - sizeof(hdr->ih_magic)-sizeof(hdr->ih_hcrc));
182 
183  hcrc = crc32_end(hcrc);
184  return (hcrc == image_get_hcrc(hdr));
185 }
186 /*----------------------------------------------------------------------------*/
188 {
189  uint32_t dcrc = crc32( 0,
190  ((unsigned char *)image_get_data(hdr)),
191  (size_t ) image_get_data_size(hdr)
192  );
193  return (dcrc == image_get_dcrc(hdr));
194 }
195 /*----------------------------------------------------------------------------*/
196 //#include <string.h>
197 /*
198 static inline int_fast8_t has_magic_number_byte(uint_fast32_t data)
199 {
200  const uint_fast8_t magic_number0 = ((IH_MAGIC>>24) & 0xFF);
201  const uint_fast8_t magic_number1 = (IH_MAGIC>>16) & 0xFF);
202  const uint_fast8_t magic_number2 = (IH_MAGIC>>8) & 0xFF);
203  const uint_fast8_t magic_number3 = (IH_MAGIC>>0) & 0xFF);
204  const uint_fast32_t magic_number0 = magic_number0 +magic_number1 <<8 + magic_number2 <<16
205  magic_number3<<24;
206  const uint_fast32_t magic_number1 = magic_number0 + magic_number1 <<8 + magic_number2 <<16;
207  const uint_fast32_t magic_number2 = magic_number0 + magic_number1 <<8 ;
208  const uint_fast32_t magic_number3 = magic_number0 ;
209 
210 
211  if (data == magic_number0)
212  return 0;
213  data = data >> 8;
214  if (data == magic_number1)
215  return 1;
216  if (data == magic_number2)
217  return 2;
218  if (data == magic_number3)
219  return 3;
220  else return (int_fast8_t) -1;
221 }
222 */
223 #define END_SEARCH 0
224 static unsigned char * memmem_magic_number(unsigned char * src, size_t size)
225 {
226  const uint_fast32_t magic_num = be32_to_cpu(IH_MAGIC);
227  while (size-- > 0){
228  if (*src == (magic_num& 0xFF)){
229  /*not worked at armtmi */
230  uint_fast32_t data = *(uint32_t *) src;
231  if (data == magic_num)
232  return src;
233  }
234  src++; /*src+=has_magic_number_byte;*/
235  }/* has not found */
236  return (unsigned char *) END_SEARCH;
237 }
238 /** search not aligned image*/
240  flash_addr_t search_end_address)
241 {
242 
243  uint32_t i = search_start_address;
244  do {
245  i = (flash_addr_t)memmem_magic_number((void *)i, search_end_address
246  - i - sizeof(image_header_t) );
247  if (i != ((flash_addr_t) END_SEARCH) )
248  if (uboot_check_image_at_addr((const void * const)i,
250  break;
251  i++;
252  } while (i != END_SEARCH);
253  return i;
254 }