3 * Copyright (C) 2007 OpenWrt.org
4 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
16 #include <unistd.h> /* for unlink() */
18 #include <getopt.h> /* for getopt() */
22 #include <endian.h> /* for __BYTE_ORDER */
23 #if defined(__CYGWIN__)
24 # include <byteswap.h>
27 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
28 # define HOST_TO_LE16(x) (x)
29 # define HOST_TO_LE32(x) (x)
30 # define LE16_TO_HOST(x) (x)
31 # define LE32_TO_HOST(x) (x)
33 # define HOST_TO_LE16(x) bswap_16(x)
34 # define HOST_TO_LE32(x) bswap_32(x)
35 # define LE16_TO_HOST(x) bswap_16(x)
36 # define LE32_TO_HOST(x) bswap_32(x)
39 #define MAX_NUM_BLOCKS 2
40 #define MAX_ARG_COUNT 32
41 #define MAX_ARG_LEN 1024
42 #define FILE_BUF_LEN (16*1024)
43 #define DEFAULT_PADC 0xFF
45 #define DEFAULT_BLOCK_ALIGN 0x10000U
47 #define CSUM_TYPE_NONE 0
49 #define CSUM_TYPE_16 2
50 #define CSUM_TYPE_32 3
61 char *file_name
; /* name of the file */
62 uint32_t file_size
; /* length of the file */
78 } __attribute__ ((packed
));
89 } __attribute__ ((packed
));
101 uint32_t max_kernel_size
;
102 uint32_t max_fs_size
;
105 #define HEADER_TYPE_NFS 0
106 #define HEADER_TYPE_CAS 1
108 #define KERNEL_SIZE_CAS (61*64*1024)
109 #define KERNEL_SIZE_NFS (52*64*1024)
110 #define FS_SIZE_NFS (9*64*1024)
112 #define CAS_MAGIC1 0x5241AA55
113 #define CAS_MAGIC2 0x524F4741
114 #define CAS_MAGIC3 0xD3F22D4E
116 /* Cellvision/SparkLAN products */
117 #define MODEL_CAS_630 0x01000000
118 #define MODEL_CAS_630W 0x01000000
119 #define MODEL_CAS_670 0x01000000
120 #define MODEL_CAS_670W 0x01000000
121 #define MODEL_NFS_101U 0x01000000
122 #define MODEL_NFS_101WU 0x01000003
123 #define MODEL_NFS_202U 0x01000001
124 #define MODEL_NFS_202WU 0x01000002
126 /* Corega products */
127 #define MODEL_CG_NSADP 0x01000020 /* NFS-101U */
128 #define MODEL_CG_NSADPCR 0x01000021 /* NFS-202U */
130 /* D-Link products */
131 #define MODEL_DCS_950 0x01010102 /* CAS-630 */
132 #define MODEL_DCS_950G 0x01020102 /* CAS-630W */
133 #define MODEL_DNS_120 0x01000030 /* NFS-101U */
134 #define MODEL_DNS_G120 0x01000032 /* NFS-101WU */
136 /* Digitus products */
137 #define MODEL_DN_16021 MODEL_CAS_630
138 #define MODEL_DN_16022 MODEL_CAS_630W
139 #define MODEL_DN_16030 MODEL_CAS_670
140 #define MODEL_DN_16031 MODEL_CAS_670W
141 #define MODEL_DN_7013 MODEL_NFS_101U
144 #define MODEL_LB_SS01TXU 0x00000000
146 /* Neu-Fusion products */
148 /* Ovislink products */
149 #define MODEL_MU_5000FS 0x01000040 /* NFS-101U */
150 #define MODEL_WL_5420CAM 0x020B0101 /* CAS-630W? */
151 #define MODEL_WL_5460CAM 0x020B0001 /* CAS-670W */
153 /* Repotec products */
155 /* Sitecom products */
156 #define MODEL_LN_350 /* unknown */
157 #define MODEL_LN_403 0x01020402
158 #define MODEL_WL_401 0x01010402
160 /* Surecom products */
161 #define MODEL_EP_4001_MM 0x01030A02 /* CAS-630 */
162 #define MODEL_EP_4002_MM 0x01020A02 /* CAS-630W */
163 #define MODEL_EP_4011_MM 0x01010A02 /* CAS-670 */
164 #define MODEL_EP_4012_MM 0x01000A02 /* CAS-670W */
165 #define MODEL_EP_9812_U /* unknown */
167 /* Trendnet products */
168 #define MODEL_TN_U100 0x01000081 /* NFS-101U */
169 #define MODEL_TN_U200 0x01000082 /* NFS-202U */
177 int keep_invalid_images
;
178 int invalid_causes_error
= 1;
179 union file_hdr header
;
181 struct image_desc kernel_image
;
182 struct image_desc fs_image
;
184 struct board_info
*board
= NULL
;
186 #define BOARD(m, n, i, ks, fs, h) { \
190 .max_kernel_size = (ks), \
191 .max_fs_size = (fs), \
195 #define BOARD_CAS(m,n,i) \
196 BOARD(m, n, i, KERNEL_SIZE_CAS, 0, HEADER_TYPE_CAS)
197 #define BOARD_NFS(m,n,i) \
198 BOARD(m, n, i, KERNEL_SIZE_NFS, FS_SIZE_NFS, HEADER_TYPE_NFS)
200 static struct board_info boards
[] = {
201 /* Cellvision/Sparklan products */
202 BOARD_CAS("CAS-630", "Cellvision CAS-630", MODEL_CAS_630
),
203 BOARD_CAS("CAS-630W", "Cellvision CAS-630W", MODEL_CAS_630W
),
204 BOARD_CAS("CAS-670", "Cellvision CAS-670", MODEL_CAS_670
),
205 BOARD_CAS("CAS-670W", "Cellvision CAS-670W", MODEL_CAS_670W
),
206 BOARD_NFS("NFS-101U", "Cellvision NFS-101U", MODEL_NFS_101U
),
207 BOARD_NFS("NFS-101WU", "Cellvision NFS-101WU", MODEL_NFS_101WU
),
208 BOARD_NFS("NFS-202U", "Cellvision NFS-202U", MODEL_NFS_202U
),
209 BOARD_NFS("NFS-202WU", "Cellvision NFS-202WU", MODEL_NFS_202WU
),
211 /* Corega products */
212 BOARD_NFS("CG-NSADP", "Corega CG-NSADP", MODEL_CG_NSADP
),
213 BOARD_NFS("CG-NSADPCR", "Corega CG-NSADPCR", MODEL_CG_NSADPCR
),
215 /* D-Link products */
216 BOARD_CAS("DCS-950", "D-Link DCS-950", MODEL_DCS_950
),
217 BOARD_CAS("DCS-950G", "D-Link DCS-950G", MODEL_DCS_950G
),
218 BOARD_NFS("DNS-120", "D-Link DNS-120", MODEL_DNS_120
),
219 BOARD_NFS("DNS-G120", "D-Link DNS-G120", MODEL_DNS_G120
),
221 /* Digitus products */
222 BOARD_NFS("DN-7013", "Digitus DN-7013", MODEL_DN_7013
),
225 BOARD_NFS("LB-SS01TXU", "Lobos LB-SS01TXU", MODEL_LB_SS01TXU
),
227 /* Ovislink products */
228 BOARD_NFS("MU-5000FS", "Ovislink MU-5000FS", MODEL_MU_5000FS
),
229 BOARD_CAS("WL-5420CAM", "Ovislink WL-5420 CAM", MODEL_WL_5420CAM
),
230 BOARD_CAS("WL-5460CAM", "Ovislink WL-5460 CAM", MODEL_WL_5460CAM
),
232 /* Sitecom products */
233 BOARD_CAS("LN-403", "Sitecom LN-403", MODEL_LN_403
),
234 BOARD_CAS("WL-401", "Sitecom WL-401", MODEL_WL_401
),
236 /* Surecom products */
237 BOARD_CAS("EP-4001-MM", "Surecom EP-4001-MM", MODEL_EP_4001_MM
),
238 BOARD_CAS("EP-4002-MM", "Surecom EP-4002-MM", MODEL_EP_4002_MM
),
239 BOARD_CAS("EP-4011-MM", "Surecom EP-4011-MM", MODEL_EP_4011_MM
),
240 BOARD_CAS("EP-4012-MM", "Surecom EP-4012-MM", MODEL_EP_4012_MM
),
242 /* TrendNET products */
243 BOARD_NFS("TN-U100", "TrendNET TN-U100", MODEL_TN_U100
),
244 BOARD_NFS("TN-U200", "TrendNET TN-U200", MODEL_TN_U200
),
252 #define ERR(fmt, ...) do { \
254 fprintf(stderr, "[%s] *** error: " fmt "\n", \
255 progname, ## __VA_ARGS__ ); \
258 #define ERRS(fmt, ...) do { \
261 fprintf(stderr, "[%s] *** error: " fmt "\n", \
262 progname, ## __VA_ARGS__, strerror(save)); \
265 #define WARN(fmt, ...) do { \
266 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
267 progname, ## __VA_ARGS__ ); \
270 #define DBG(lev, fmt, ...) do { \
271 if (verblevel < lev) \
273 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
277 #define ERR_INVALID_IMAGE -2
282 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
283 struct board_info
*board
;
285 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
289 " -B <board> create image for the board specified with <board>.\n"
290 " valid <board> values:\n"
292 for (board
= boards
; board
->model
!= NULL
; board
++){
295 board
->model
, board
->name
);
298 " -d don't throw error on invalid images\n"
299 " -k keep invalid images\n"
300 " -K <file> add kernel to the image\n"
301 " -C <file> add custom filesystem to the image\n"
302 " -h show this screen\n"
304 " <file> write output to the file <file>\n"
310 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
315 ret
= (base
+ alignment
- 1);
316 ret
&= ~(alignment
-1);
328 str2u32(char *arg
, uint32_t *val
)
334 t
= strtoul(arg
, &err
, 0);
335 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
345 str2u16(char *arg
, uint16_t *val
)
351 t
= strtoul(arg
, &err
, 0);
352 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
361 str2u8(char *arg
, uint8_t *val
)
367 t
= strtoul(arg
, &err
, 0);
368 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
377 parse_arg(char *arg
, char *buf
, char *argv
[])
385 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
398 if (argl
>= MAX_ARG_LEN
) {
399 /* argument is too long */
400 argl
= MAX_ARG_LEN
-1;
403 memcpy(buf
, arg
, argl
);
406 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
407 tok
= strsep(ap
, ":");
412 else if (tok
[0] == '\0') {
425 required_arg(char c
, char *arg
)
427 if (arg
== NULL
|| *arg
!= '-')
430 ERR("option -%c requires an argument\n", c
);
436 is_empty_arg(char *arg
)
447 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
449 for ( ; len
> 0; len
--) {
456 csum8_get(struct csum_state
*css
)
466 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
471 t
= css
->tmp
+ (p
[0]<<8);
472 css
->val
+= LE16_TO_HOST(t
);
478 for ( ; len
> 1; len
-= 2, p
+=2 ) {
479 t
= p
[0] + (p
[1] << 8);
480 css
->val
+= LE16_TO_HOST(t
);
491 csum16_get(struct csum_state
*css
)
495 csum16_update(&pad
, 1, css
);
496 return ~css
->val
+ 1;
500 csum32_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
504 for ( ; len
> 3; len
-= 4, p
+= 4 ) {
505 t
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
511 csum32_get(struct csum_state
*css
)
518 csum_init(struct csum_state
*css
, int size
)
527 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
531 csum8_update(p
,len
,css
);
534 csum16_update(p
,len
,css
);
537 csum32_update(p
,len
,css
);
544 csum_get(struct csum_state
*css
)
550 ret
= csum8_get(css
);
553 ret
= csum16_get(css
);
556 ret
= csum32_get(css
);
564 * routines to write data to the output file
567 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
568 struct csum_state
*css
)
572 fwrite(data
, len
, 1, outfile
);
574 ERRS("unable to write output file");
579 csum_update(data
, len
, css
);
587 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
588 struct csum_state
*css
)
591 size_t buflen
= sizeof(buf
);
594 memset(buf
, padc
, buflen
);
599 err
= write_out_data(outfile
, buf
, buflen
, css
);
611 image_stat_file(struct image_desc
*desc
)
616 if (desc
->file_name
== NULL
)
619 err
= stat(desc
->file_name
, &st
);
621 ERRS("stat failed on %s", desc
->file_name
);
625 if (st
.st_size
> desc
->out_size
) {
626 WARN("file %s is too big, will be truncated to %d bytes\n",
627 desc
->file_name
, desc
->out_size
);
628 desc
->file_size
= desc
->out_size
;
629 return ERR_INVALID_IMAGE
;
633 desc
->file_size
= st
.st_size
;
634 desc
->out_size
= align(desc
->file_size
,1);
640 image_writeout_file(FILE *outfile
, struct image_desc
*desc
,
641 struct csum_state
*css
)
643 char buf
[FILE_BUF_LEN
];
644 size_t buflen
= sizeof(buf
);
649 if (desc
->file_name
== NULL
)
652 if (desc
->file_size
== 0)
656 f
= fopen(desc
->file_name
,"r");
658 ERRS("unable to open file: %s", desc
->file_name
);
662 len
= desc
->file_size
;
667 /* read data from source file */
669 fread(buf
, buflen
, 1, f
);
671 ERRS("unable to read from file: %s", desc
->file_name
);
676 res
= write_out_data(outfile
, buf
, buflen
, css
);
689 image_writeout(FILE *outfile
, struct image_desc
*desc
)
692 struct csum_state css
;
697 if (!desc
->file_size
)
700 DBG(2, "writing image, file=%s, file_size=%d\n",
701 desc
->file_name
, desc
->file_size
);
703 csum_init(&css
, CSUM_TYPE_32
);
705 res
= image_writeout_file(outfile
, desc
, &css
);
709 /* write padding data if neccesary */
710 padlen
= desc
->out_size
- desc
->file_size
;
711 DBG(1,"padding desc, length=%d", padlen
);
712 res
= write_out_padding(outfile
, padlen
, desc
->padc
, &css
);
714 desc
->csum
= csum_get(&css
);
721 write_out_header(FILE *outfile
)
727 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
728 ERRS("fseek failed on output file");
732 switch (board
->header_type
) {
733 case HEADER_TYPE_CAS
:
734 tmp
.cas
.type
= HOST_TO_LE32(header
.cas
.type
);
735 tmp
.cas
.id
= HOST_TO_LE32(header
.cas
.id
);
736 tmp
.cas
.kernel_offs
= HOST_TO_LE32(sizeof(tmp
.cas
));
737 tmp
.cas
.kernel_size
= HOST_TO_LE32(kernel_image
.out_size
);
738 tmp
.cas
.kernel_csum
= HOST_TO_LE32(kernel_image
.csum
);
739 tmp
.cas
.magic1
= HOST_TO_LE32(CAS_MAGIC1
);
740 tmp
.cas
.magic2
= HOST_TO_LE32(CAS_MAGIC2
);
741 tmp
.cas
.magic3
= HOST_TO_LE32(CAS_MAGIC3
);
742 res
= write_out_data(outfile
, (uint8_t *)&tmp
.cas
,
743 sizeof(tmp
.cas
), NULL
);
745 case HEADER_TYPE_NFS
:
746 tmp
.nfs
.type
= HOST_TO_LE32(header
.nfs
.type
);
747 tmp
.nfs
.id
= HOST_TO_LE32(header
.nfs
.id
);
748 tmp
.nfs
.kernel_offs
= HOST_TO_LE32(sizeof(tmp
.nfs
));
749 tmp
.nfs
.kernel_size
= HOST_TO_LE32(kernel_image
.out_size
);
750 tmp
.nfs
.kernel_csum
= HOST_TO_LE32(kernel_image
.csum
);
751 tmp
.nfs
.fs_offs
= HOST_TO_LE32(sizeof(tmp
.nfs
)
752 + kernel_image
.out_size
);
753 tmp
.nfs
.fs_size
= HOST_TO_LE32(fs_image
.out_size
);
754 tmp
.nfs
.fs_csum
= HOST_TO_LE32(fs_image
.csum
);
755 res
= write_out_data(outfile
, (uint8_t *)&tmp
.nfs
,
756 sizeof(tmp
.nfs
), NULL
);
764 write_out_images(FILE *outfile
)
766 struct image_desc
*desc
;
769 res
= image_writeout(outfile
, &kernel_image
);
773 res
= image_writeout(outfile
, &fs_image
);
782 find_board(char *model
)
784 struct board_info
*ret
;
785 struct board_info
*board
;
788 for (board
= boards
; board
->model
!= NULL
; board
++){
789 if (strcasecmp(model
, board
->model
) == 0) {
800 parse_opt_board(char ch
, char *arg
)
803 DBG(1,"parsing board option: -%c %s", ch
, arg
);
806 ERR("only one board option allowed");
810 if (required_arg(ch
, arg
))
813 board
= find_board(arg
);
815 ERR("invalid/unknown board specified: %s", arg
);
819 switch (board
->header_type
) {
820 case HEADER_TYPE_CAS
:
821 header
.cas
.type
= HEADER_TYPE_CAS
;
822 header
.cas
.id
= board
->id
;
824 case HEADER_TYPE_NFS
:
825 header
.nfs
.type
= HEADER_TYPE_NFS
;
826 header
.nfs
.id
= board
->id
;
829 ERR("internal error, unknown header type\n");
838 parse_opt_image(char ch
, char *arg
)
840 char buf
[MAX_ARG_LEN
];
841 char *argv
[MAX_ARG_COUNT
];
844 struct image_desc
*desc
= NULL
;
849 if (kernel_image
.file_name
) {
850 WARN("only one kernel option allowed");
853 desc
= &kernel_image
;
856 if (fs_image
.file_name
) {
857 WARN("only one fs option allowed");
867 argc
= parse_arg(arg
, buf
, argv
);
871 if (!is_empty_arg(p
)) {
872 desc
->file_name
= strdup(p
);
873 if (desc
->file_name
== NULL
) {
874 ERR("not enough memory");
878 ERR("no file specified for option %c", ch
);
889 struct image_desc
*desc
;
894 kernel_image
.out_size
= board
->max_kernel_size
;
895 kernel_image
.padc
= DEFAULT_PADC
;
896 res
= image_stat_file(&kernel_image
);
900 if (!fs_image
.file_name
)
903 fs_image
.out_size
= board
->max_fs_size
;
904 fs_image
.padc
= DEFAULT_PADC
;
905 res
= image_stat_file(&fs_image
);
914 main(int argc
, char *argv
[])
916 int optinvalid
= 0; /* flag for invalid option */
922 progname
=basename(argv
[0]);
924 opterr
= 0; /* could not print standard getopt error messages */
928 c
= getopt(argc
, argv
, "B:C:dhK:r:vw:x:");
934 optinvalid
= parse_opt_board(c
,optarg
);
937 invalid_causes_error
= 0;
941 optinvalid
= parse_opt_image(c
,optarg
);
944 keep_invalid_images
= 1;
956 if (optinvalid
!= 0 ){
957 ERR("invalid option: -%c", optopt
);
963 ERR("no board specified");
967 if (optind
== argc
) {
968 ERR("no output file specified");
972 ofname
= argv
[optind
++];
975 ERR("invalid option: %s", argv
[optind
]);
979 res
= process_images();
980 if (res
== ERR_FATAL
)
983 if (res
== ERR_INVALID_IMAGE
) {
984 if (invalid_causes_error
)
987 if (keep_invalid_images
== 0) {
988 WARN("generation of invalid images disabled", ofname
);
992 WARN("generating invalid image", ofname
);
995 outfile
= fopen(ofname
, "w");
996 if (outfile
== NULL
) {
997 ERRS("could not open \"%s\" for writing", ofname
);
1002 if (write_out_header(outfile
) != 0) {
1007 if (write_out_images(outfile
) != 0) {
1012 if (write_out_header(outfile
) != 0) {
1017 DBG(1,"Image file %s completed.", ofname
);
1022 if (res
== ERR_FATAL
) {
1026 if (res
== ERR_FATAL
)
1027 return EXIT_FAILURE
;
1029 return EXIT_SUCCESS
;
This page took 0.083648 seconds and 5 git commands to generate.