2 * Copyright (C) 2006 - 2008 Neuros Technology LLC.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; only support version 2 of the License.
8 * This program is distributed in the hope that, in addition to its
9 * original purpose to support Neuros hardware, it will be useful
10 * otherwise, but WITHOUT ANY WARRANTY; without even the implied
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *****************************************************************************/
21 * This file for packaging some images to one package.
22 * The package is named r3.upk.
27 * change follow the new UPK structure
38 #define VER_LIMIT_LEN 14
40 #define SAFE_SIZE (250*1024*1024) //250MB
42 static package_header_t p_head;
43 static image_info_t i_info[10];
45 static void print_image_info(image_info_t *iif)
47 printf("iif->i_type: %x\n", iif->i_type);
48 printf("iif->i_imagesize: %d\n", iif->i_imagesize);
49 printf("iif->i_startaddr_p: %d\n", iif->i_startaddr_p);
50 printf("iif->i_startaddr_f: %x\n", iif->i_startaddr_f);
51 printf("iif->i_endaddr_f: %x\n", iif->i_endaddr_f);
52 printf("iif->i_name: %s\n", iif->i_name);
53 printf("iif->i_version: %s\n", iif->i_version);
56 static void print_head_info(void)
58 package_header_t *phd = &p_head;
60 printf("phd->p_headsize: %x\n", phd->p_headsize);
61 printf("phd->p_reserve: %x\n", phd->p_reserve);
62 printf("phd->p_headcrc: %x\n", phd->p_headcrc);
63 printf("phd->p_datasize: %d\n", phd->p_datasize);
64 printf("phd->p_datacrc: %x\n", phd->p_datacrc);
65 printf("phd->p_name: %s\n", phd->p_name);
66 printf("phd->p_vuboot: %s\n", phd->p_vuboot);
67 printf("phd->p_vkernel: %s\n", phd->p_vkernel);
68 printf("phd->p_vrootfs: %s\n", phd->p_vrootfs);
69 printf("phd->p_imagenum: %x\n", phd->p_imagenum);
72 static void print_version_info(version_info *ver_t)
74 printf("ver_t->upk_desc: %s\n", ver_t->upk_desc);
75 printf("ver_t->pack_id: %s\n", ver_t->pack_id);
76 printf("ver_t->os_ver : %s\n", ver_t->os_ver);
77 printf("ver_t->app_ver: %s\n", ver_t->app_ver);
80 static int pack_firmware(FILE *fp_w, uint32 offst, int num, char *name[])
84 uint32 curptr, extcrc;
86 package_header_t *phd = &p_head;
89 /* read version file */
90 if ((fp_r = fopen(UBOOT_VER_FILE, "rb")) == NULL)
92 printf("Can't open uboot version file: %s\n", UBOOT_VER_FILE);
98 if (feof(fp_r)) break;
99 if (j > VER_LIMIT_LEN+1)
101 printf("uboot version can't be longer than 14\n");
104 phd->p_vuboot[j] = fgetc(fp_r);
105 if ((phd->p_vuboot[j]==0x0d) || (phd->p_vuboot[j]==0x0a))
106 phd->p_vuboot[j] = '\0';
111 if ((fp_r = fopen(KERNEL_VER_FILE, "rb")) == NULL)
113 printf("Can't open kernel version file: %s\n", KERNEL_VER_FILE);
119 if (feof(fp_r)) break;
120 if (j > VER_LIMIT_LEN+1)
122 printf("kernel version can't be longer than 14\n");
125 phd->p_vkernel[j]=fgetc(fp_r);
126 if ((phd->p_vkernel[j]==0x0d) || (phd->p_vkernel[j]==0x0a))
127 phd->p_vkernel[j] = '\0';
132 if ((fp_r = fopen(ROOTFS_VER_FILE, "rb")) == NULL)
134 printf("Can't open rootfs version file: %s\n", ROOTFS_VER_FILE);
140 if (feof(fp_r)) break;
141 if (j > VER_LIMIT_LEN+1)
143 printf("rootfs version can't be longer than 14\n");
146 phd->p_vrootfs[j] = fgetc(fp_r);
147 if ((phd->p_vrootfs[j]==0x0d) ||(phd->p_vrootfs[j]==0x0a))
148 phd->p_vrootfs[j] = '\0';
153 phd->p_imagenum = (uint8)num;
154 phd->p_headsize = sizeof(package_header_t) + phd->p_imagenum * sizeof(image_info_t);
156 /* Bit[3] use to indicate osd2.0 package */
157 phd->p_reserve = 0x08;
163 curptr = phd->p_headsize + sizeof(version_info);
165 for (i=0; i < phd->p_imagenum; i++)
169 if (strncmp(name[i], ROOTFS_FILE_NAME, strlen(ROOTFS_FILE_NAME)) == 0)
171 iif->i_type = IH_TYPE_ROOTFS;
172 strncpy((char *)iif->i_name, ROOTFS_FILE_NAME, NAMELEN-1);
174 if ((fp_r = fopen(ROOTFS_VER_FILE, "rb")) == NULL)
176 printf("Can't open kernel version file: %s\n", ROOTFS_VER_FILE);
179 for (j = 0; j < sizeof(iif->i_version); j++)
181 if (feof(fp_r)) break;
182 iif->i_version[j] = fgetc(fp_r);
183 if ((iif->i_version[j]==0x0d) || (iif->i_version[j]==0x0a))
184 iif->i_version[j] = '\0';
188 else if (strncmp(name[i], KERNEL_FILE_NAME, strlen(KERNEL_FILE_NAME)) == 0)
190 iif->i_type = IH_TYPE_KERNEL;
191 strncpy((char *)iif->i_name, KERNEL_FILE_NAME, NAMELEN-1);
193 if ((fp_r = fopen(KERNEL_VER_FILE, "rb")) == NULL)
195 printf("Can't open kernel version file: %s\n", KERNEL_VER_FILE);
198 for (j = 0; j < sizeof(iif->i_version); j++)
200 if (feof(fp_r)) break;
201 iif->i_version[j] = fgetc(fp_r);
202 if ((iif->i_version[j]==0x0d) ||(iif->i_version[j]==0x0a))
203 iif->i_version[j] = '\0';
207 else if (strncmp(name[i], UBOOT_FILE_NAME, strlen(UBOOT_FILE_NAME)) == 0)
209 iif->i_type = IH_TYPE_UBOOT;
210 strncpy((char *)iif->i_name, UBOOT_FILE_NAME, NAMELEN-1);
212 if ((fp_r = fopen(UBOOT_VER_FILE, "rb")) == NULL)
214 printf("Can't open uboot version file: %s\n", UBOOT_VER_FILE);
217 for (j = 0; j < sizeof(iif->i_version); j++)
219 if (feof(fp_r)) break;
220 iif->i_version[j] = fgetc(fp_r);
221 if ((iif->i_version[j]==0x0d)|| (iif->i_version[j]==0x0a))
222 iif->i_version[j] = '\0';
226 else if (strncmp(name[i], SCRIPT_FILE_NAME, strlen(SCRIPT_FILE_NAME)) == 0)
228 iif->i_type = IH_TYPE_SCRIPT;
229 strncpy((char *)iif->i_name, SCRIPT_FILE_NAME, NAMELEN-1);
232 /* address in flash*/
236 iif->i_startaddr_f = ROOTFS_ADDR_START;
237 iif->i_endaddr_f = ROOTFS_ADDR_END;
240 iif->i_startaddr_f = KERNEL_ADDR_START;
241 iif->i_endaddr_f = KERNEL_ADDR_END;
244 iif->i_startaddr_f = UBOOT_ADDR_START;
245 iif->i_endaddr_f = UBOOT_ADDR_END;
250 printf("un-handle image type\n");
254 /* write whole image to package and calculate the imagesize*/
255 iif->i_imagesize = 0;
257 if ((fp_r = fopen(name[i], "rb")) == NULL)
259 printf("can't open file: %s\n", name[i]);
263 fseek(fp_w, offst+curptr,SEEK_SET);
269 phd->p_datacrc = crc32(phd->p_datacrc,(uint8 *)&ch, 1);
274 iif->i_startaddr_p = curptr-sizeof(version_info);
275 curptr += iif->i_imagesize;
276 phd->p_datasize += iif->i_imagesize;
278 print_image_info(iif); /* print iff*/
280 /*write image info */
281 fseek(fp_w, offst+sizeof(version_info)+sizeof(package_header_t)+i*sizeof(image_info_t), SEEK_SET);
282 if (fwrite(iif, sizeof(image_info_t), 1, fp_w) != 1)
284 printf("can not write iif into package\n");
289 /* write package head*/
290 phd->p_headcrc = crc32(phd->p_headcrc, (uint8 *)phd, sizeof(package_header_t));
291 phd->p_headcrc = crc32(phd->p_headcrc, (uint8 *)i_info, phd->p_imagenum*sizeof(image_info_t));
293 print_head_info(); /* print phd */
295 fseek(fp_w, offst+sizeof(version_info), SEEK_SET);
296 if (fwrite((uint8 *)phd, sizeof(package_header_t), 1, fp_w) != 1)
298 printf("can not write head into package");
309 static int pack_ver_info(FILE *fp_w, uint32 offset, char *desc)
315 memset((char *)&ver_t, 0, sizeof(version_info));
317 if (strlen(desc) >= DESCLEN)
319 printf("The upk_desc is too long\n");
322 strncpy((char *)ver_t.upk_desc, desc, DESCLEN-1);
323 strncpy((char *)ver_t.pack_id, (char *)PACKAGE_ID, NAMELEN-1);
324 strncpy((char *)ver_t.os_ver, "0.00", VERLEN-1);
325 strncpy((char *)ver_t.app_ver, "0.00", VERLEN-1);
327 if ((fp_r = fopen(KERNEL_VER_FILE, "rb")) == NULL)
329 printf("Can't open OS version file: %s\n", KERNEL_VER_FILE);
332 for (i = 0; i < sizeof(ver_t.os_ver); i++)
334 if (feof(fp_r)) break;
335 ver_t.os_ver[i] = fgetc(fp_r);
336 if ((ver_t.os_ver[i]==0x0d) || (ver_t.os_ver[i]==0x0a))
337 ver_t.os_ver[i] = '\0';
341 if ((fp_r = fopen(ROOTFS_VER_FILE, "rb")) == NULL)
343 printf("Can't open App version file: %s\n", ROOTFS_VER_FILE);
346 for (i = 0; i < sizeof(ver_t.app_ver); i++)
348 if (feof(fp_r)) break;
349 ver_t.app_ver[i] = fgetc(fp_r);
350 if ((ver_t.app_ver[i]==0x0d) || (ver_t.app_ver[i]==0x0a))
351 ver_t.app_ver[i] = '\0';
355 fseek(fp_w, 0, SEEK_SET);
356 if (fwrite((uint8 *)&ver_t, sizeof(version_info), 1, fp_w) != 1)
358 printf("can not write the version struct into package\n");
362 print_version_info(&ver_t);
367 /* argv[1] packet name
368 argv[2] upk descpription
372 argv[5] rootfs image*/
373 int main(int argc, char *argv[])
377 package_header_t *phd = &p_head;
380 printf("\npackage tool version %s \n", VERSION);
382 strncpy((char *)phd->p_name, argv[1], NAMELEN-1);
383 if ((fp_w = fopen((char *)phd->p_name, "wb+")) == NULL)
385 printf("Can't open %s\n",phd->p_name);
389 /* packet firmware to package */
390 if (pack_firmware(fp_w, hw_len, 4, &argv[3]) != 0)
392 /* packet upk_desc and version info */
393 if (pack_ver_info(fp_w, hw_len+phd->p_headsize, argv[2]) != 0)
398 stat((char *)phd->p_name, &buf);
399 if (buf.st_size > SAFE_SIZE)
401 printf("Warning!!!!! The upk size is larger than the safe size\n");