4 * Copyright (C) 2007 OpenWrt.org
5 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
17 #include <unistd.h> /* for unlink() */
19 #include <getopt.h> /* for getopt() */
23 #include <endian.h> /* for __BYTE_ORDER */
24 #if defined(__CYGWIN__)
25 # include <byteswap.h>
28 #if (__BYTE_ORDER == __LITTLE_ENDIAN)
29 # define HOST_TO_LE16(x) (x)
30 # define HOST_TO_LE32(x) (x)
31 # define LE16_TO_HOST(x) (x)
32 # define LE32_TO_HOST(x) (x)
34 # define HOST_TO_LE16(x) bswap_16(x)
35 # define HOST_TO_LE32(x) bswap_32(x)
36 # define LE16_TO_HOST(x) bswap_16(x)
37 # define LE32_TO_HOST(x) bswap_32(x)
40 #define MAX_NUM_BLOCKS 2
41 #define MAX_ARG_COUNT 32
42 #define MAX_ARG_LEN 1024
43 #define FILE_BUF_LEN (16*1024)
44 #define DEFAULT_PADC 0xFF
46 #define DEFAULT_BLOCK_ALIGN 0x10000U
48 #define CSUM_TYPE_NONE 0
50 #define CSUM_TYPE_16 2
51 #define CSUM_TYPE_32 3
62 char *file_name
; /* name of the file */
63 uint32_t file_size
; /* length of the file */
79 } __attribute__ ((packed
));
90 } __attribute__ ((packed
));
102 uint32_t max_kernel_size
;
103 uint32_t max_fs_size
;
106 #define HEADER_TYPE_NFS 0
107 #define HEADER_TYPE_CAS 1
109 #define KERNEL_SIZE_CAS (61*64*1024)
110 #define KERNEL_SIZE_NFS (52*64*1024)
111 #define FS_SIZE_NFS (9*64*1024)
113 #define CAS_MAGIC1 0x5241AA55
114 #define CAS_MAGIC2 0x524F4741
115 #define CAS_MAGIC3 0xD3F22D4E
117 /* Cellvision/SparkLAN products */
118 #define MODEL_CAS_630 0x01000000
119 #define MODEL_CAS_630W 0x01000000
120 #define MODEL_CAS_670 0x01000000
121 #define MODEL_CAS_670W 0x01000000
122 #define MODEL_NFS_101U 0x01000000
123 #define MODEL_NFS_101WU 0x01000003
124 #define MODEL_NFS_202U 0x01000001
125 #define MODEL_NFS_202WU 0x01000002
127 /* Corega products */
128 #define MODEL_CG_NSADP 0x01000020 /* NFS-101U */
129 #define MODEL_CG_NSADPCR 0x01000021 /* NFS-202U */
131 /* D-Link products */
132 #define MODEL_DCS_950 0x01010102 /* CAS-630 */
133 #define MODEL_DCS_950G 0x01020102 /* CAS-630W */
134 #define MODEL_DNS_120 0x01000030 /* NFS-101U */
135 #define MODEL_DNS_G120 0x01000032 /* NFS-101WU */
137 /* Digitus products */
138 #define MODEL_DN_16021 MODEL_CAS_630
139 #define MODEL_DN_16022 MODEL_CAS_630W
140 #define MODEL_DN_16030 MODEL_CAS_670
141 #define MODEL_DN_16031 MODEL_CAS_670W
142 #define MODEL_DN_7013 MODEL_NFS_101U
145 #define MODEL_LB_SS01TXU 0x00000000
147 /* Neu-Fusion products */
149 /* Ovislink products */
150 #define MODEL_MU_5000FS 0x01000040 /* NFS-101U */
152 /* Repotec products */
154 /* Sitecom products */
155 #define MODEL_LN_350 /* unknown */
156 #define MODEL_LN_403 0x01020402
157 #define MODEL_WL_401 0x01010402
159 /* Surecom products */
160 #define MODEL_EP_4001_MM 0x01030A02 /* CAS-630 */
161 #define MODEL_EP_4002_MM 0x01020A02 /* CAS-630W */
162 #define MODEL_EP_4011_MM 0x01010A02 /* CAS-670 */
163 #define MODEL_EP_4012_MM 0x01000A02 /* CAS-670W */
164 #define MODEL_EP_9812_U /* unknown */
166 /* Trendnet products */
167 #define MODEL_TN_U100 0x01000081 /* NFS-101U */
168 #define MODEL_TN_U200 0x01000081 /* NFS-202U */
176 int keep_invalid_images
;
177 int invalid_causes_error
= 1;
178 union file_hdr header
;
180 struct image_desc kernel_image
;
181 struct image_desc fs_image
;
183 struct board_info
*board
= NULL
;
185 #define BOARD(m, n, i, ks, fs, h) { \
189 .max_kernel_size = (ks), \
190 .max_fs_size = (fs), \
194 #define BOARD_CAS(m,n,i) \
195 BOARD(m, n, i, KERNEL_SIZE_CAS, 0, HEADER_TYPE_CAS)
196 #define BOARD_NFS(m,n,i) \
197 BOARD(m, n, i, KERNEL_SIZE_NFS, FS_SIZE_NFS, HEADER_TYPE_NFS)
199 static struct board_info boards
[] = {
200 /* Cellvision/Sparklan products */
201 BOARD_CAS("CAS-630", "Cellvision CAS-630", MODEL_CAS_630
),
202 BOARD_CAS("CAS-630W", "Cellvision CAS-630W", MODEL_CAS_630W
),
203 BOARD_CAS("CAS-670", "Cellvision CAS-670", MODEL_CAS_670
),
204 BOARD_CAS("CAS-670W", "Cellvision CAS-670W", MODEL_CAS_670W
),
205 BOARD_NFS("NFS-101U", "Cellvision NFS-101U", MODEL_NFS_101U
),
206 BOARD_NFS("NFS-101WU", "Cellvision NFS-101WU", MODEL_NFS_101WU
),
207 BOARD_NFS("NFS-202U", "Cellvision NFS-202U", MODEL_NFS_202U
),
208 BOARD_NFS("NFS-202WU", "Cellvision NFS-202WU", MODEL_NFS_202WU
),
210 /* Corega products */
211 BOARD_NFS("CG-NSADP", "Corega CG-NSADP", MODEL_CG_NSADP
),
212 BOARD_NFS("CG-NSADPCR", "Corega CG-NSADPCR", MODEL_CG_NSADPCR
),
214 /* D-Link products */
215 BOARD_CAS("DCS-950", "D-Link DCS-950", MODEL_DCS_950
),
216 BOARD_CAS("DCS-950G", "D-Link DCS-950G", MODEL_DCS_950G
),
217 BOARD_NFS("DNS-120", "D-Link DNS-120", MODEL_DNS_120
),
218 BOARD_NFS("DNS-G120", "D-Link DNS-G120", MODEL_DNS_G120
),
220 /* Digitus products */
221 BOARD_NFS("DN-7013", "Digitus DN-7013", MODEL_DN_7013
),
224 BOARD_NFS("LB-SS01TXU", "Lobos LB-SS01TXU", MODEL_LB_SS01TXU
),
226 /* Ovislink products */
227 BOARD_NFS("MU-5000FS", "Ovislink MU-5000FS", MODEL_MU_5000FS
),
229 /* Sitecom products */
230 BOARD_CAS("LN-403", "Sitecom LN-403", MODEL_LN_403
),
231 BOARD_CAS("WL-401", "Sitecom WL-401", MODEL_WL_401
),
233 /* Surecom products */
234 BOARD_CAS("EP-4001-MM", "Surecom EP-4001-MM", MODEL_EP_4001_MM
),
235 BOARD_CAS("EP-4002-MM", "Surecom EP-4002-MM", MODEL_EP_4002_MM
),
236 BOARD_CAS("EP-4011-MM", "Surecom EP-4011-MM", MODEL_EP_4011_MM
),
237 BOARD_CAS("EP-4012-MM", "Surecom EP-4012-MM", MODEL_EP_4012_MM
),
239 /* TrendNET products */
240 BOARD_NFS("TN-U100", "TrendNET TN-U100", MODEL_TN_U100
),
241 BOARD_NFS("TN-U200", "TrendNET TN-U200", MODEL_TN_U200
),
249 #define ERR(fmt, ...) do { \
251 fprintf(stderr, "[%s] *** error: " fmt "\n", \
252 progname, ## __VA_ARGS__ ); \
255 #define ERRS(fmt, ...) do { \
258 fprintf(stderr, "[%s] *** error: " fmt "\n", \
259 progname, ## __VA_ARGS__, strerror(save)); \
262 #define WARN(fmt, ...) do { \
263 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
264 progname, ## __VA_ARGS__ ); \
267 #define DBG(lev, fmt, ...) do { \
268 if (verblevel < lev) \
270 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
274 #define ERR_INVALID_IMAGE -2
279 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
280 struct board_info
*board
;
282 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
286 " -B <board> create image for the board specified with <board>.\n"
287 " valid <board> values:\n"
289 for (board
= boards
; board
->model
!= NULL
; board
++){
292 board
->model
, board
->name
);
295 " -d don't throw error on invalid images\n"
296 " -k keep invalid images\n"
297 " -K <file> add kernel to the image\n"
298 " -C <file> add custom filesystem to the image\n"
299 " -h show this screen\n"
301 " <file> write output to the file <file>\n"
307 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
312 ret
= (base
+ alignment
- 1);
313 ret
&= ~(alignment
-1);
325 str2u32(char *arg
, uint32_t *val
)
331 t
= strtoul(arg
, &err
, 0);
332 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
342 str2u16(char *arg
, uint16_t *val
)
348 t
= strtoul(arg
, &err
, 0);
349 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
358 str2u8(char *arg
, uint8_t *val
)
364 t
= strtoul(arg
, &err
, 0);
365 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
374 parse_arg(char *arg
, char *buf
, char *argv
[])
382 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
395 if (argl
>= MAX_ARG_LEN
) {
396 /* argument is too long */
397 argl
= MAX_ARG_LEN
-1;
400 memcpy(buf
, arg
, argl
);
403 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
404 tok
= strsep(ap
, ":");
409 else if (tok
[0] == '\0') {
422 required_arg(char c
, char *arg
)
424 if (arg
== NULL
|| *arg
!= '-')
427 ERR("option -%c requires an argument\n", c
);
433 is_empty_arg(char *arg
)
444 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
446 for ( ; len
> 0; len
--) {
453 csum8_get(struct csum_state
*css
)
463 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
468 t
= css
->tmp
+ (p
[0]<<8);
469 css
->val
+= LE16_TO_HOST(t
);
475 for ( ; len
> 1; len
-= 2, p
+=2 ) {
476 t
= p
[0] + (p
[1] << 8);
477 css
->val
+= LE16_TO_HOST(t
);
488 csum16_get(struct csum_state
*css
)
492 csum16_update(&pad
, 1, css
);
493 return ~css
->val
+ 1;
497 csum32_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
501 for ( ; len
> 3; len
-= 4, p
+= 4 ) {
502 t
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
508 csum32_get(struct csum_state
*css
)
515 csum_init(struct csum_state
*css
, int size
)
524 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
528 csum8_update(p
,len
,css
);
531 csum16_update(p
,len
,css
);
534 csum32_update(p
,len
,css
);
541 csum_get(struct csum_state
*css
)
547 ret
= csum8_get(css
);
550 ret
= csum16_get(css
);
553 ret
= csum32_get(css
);
561 * routines to write data to the output file
564 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
565 struct csum_state
*css
)
569 fwrite(data
, len
, 1, outfile
);
571 ERRS("unable to write output file");
576 csum_update(data
, len
, css
);
584 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
585 struct csum_state
*css
)
588 size_t buflen
= sizeof(buf
);
591 memset(buf
, padc
, buflen
);
596 err
= write_out_data(outfile
, buf
, buflen
, css
);
608 image_stat_file(struct image_desc
*desc
)
613 if (desc
->file_name
== NULL
)
616 err
= stat(desc
->file_name
, &st
);
618 ERRS("stat failed on %s", desc
->file_name
);
622 if (st
.st_size
> desc
->out_size
) {
623 WARN("file %s is too big, will be truncated to %d bytes\n",
624 desc
->file_name
, desc
->out_size
);
625 desc
->file_size
= desc
->out_size
;
626 return ERR_INVALID_IMAGE
;
630 desc
->file_size
= st
.st_size
;
631 desc
->out_size
= align(desc
->file_size
,1);
637 image_writeout_file(FILE *outfile
, struct image_desc
*desc
,
638 struct csum_state
*css
)
640 char buf
[FILE_BUF_LEN
];
641 size_t buflen
= sizeof(buf
);
646 if (desc
->file_name
== NULL
)
649 if (desc
->file_size
== 0)
653 f
= fopen(desc
->file_name
,"r");
655 ERRS("unable to open file: %s", desc
->file_name
);
659 len
= desc
->file_size
;
664 /* read data from source file */
666 fread(buf
, buflen
, 1, f
);
668 ERRS("unable to read from file: %s", desc
->file_name
);
673 res
= write_out_data(outfile
, buf
, buflen
, css
);
686 image_writeout(FILE *outfile
, struct image_desc
*desc
)
689 struct csum_state css
;
694 if (!desc
->file_size
)
697 DBG(2, "writing image, file=%s, file_size=%d\n",
698 desc
->file_name
, desc
->file_size
);
700 csum_init(&css
, CSUM_TYPE_32
);
702 res
= image_writeout_file(outfile
, desc
, &css
);
706 /* write padding data if neccesary */
707 padlen
= desc
->out_size
- desc
->file_size
;
708 DBG(1,"padding desc, length=%d", padlen
);
709 res
= write_out_padding(outfile
, padlen
, desc
->padc
, &css
);
711 desc
->csum
= csum_get(&css
);
718 write_out_header(FILE *outfile
)
724 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
725 ERRS("fseek failed on output file");
729 switch (board
->header_type
) {
730 case HEADER_TYPE_CAS
:
731 tmp
.cas
.type
= HOST_TO_LE32(header
.cas
.type
);
732 tmp
.cas
.id
= HOST_TO_LE32(header
.cas
.id
);
733 tmp
.cas
.kernel_offs
= HOST_TO_LE32(sizeof(tmp
.cas
));
734 tmp
.cas
.kernel_size
= HOST_TO_LE32(kernel_image
.out_size
);
735 tmp
.cas
.kernel_csum
= HOST_TO_LE32(kernel_image
.csum
);
736 tmp
.cas
.magic1
= HOST_TO_LE32(CAS_MAGIC1
);
737 tmp
.cas
.magic2
= HOST_TO_LE32(CAS_MAGIC2
);
738 tmp
.cas
.magic3
= HOST_TO_LE32(CAS_MAGIC3
);
739 res
= write_out_data(outfile
, (uint8_t *)&tmp
.cas
,
740 sizeof(tmp
.cas
), NULL
);
742 case HEADER_TYPE_NFS
:
743 tmp
.nfs
.type
= HOST_TO_LE32(header
.nfs
.type
);
744 tmp
.nfs
.id
= HOST_TO_LE32(header
.nfs
.id
);
745 tmp
.nfs
.kernel_offs
= HOST_TO_LE32(sizeof(tmp
.nfs
));
746 tmp
.nfs
.kernel_size
= HOST_TO_LE32(kernel_image
.out_size
);
747 tmp
.nfs
.kernel_csum
= HOST_TO_LE32(kernel_image
.csum
);
748 tmp
.nfs
.fs_offs
= HOST_TO_LE32(sizeof(tmp
.nfs
)
749 + kernel_image
.out_size
);
750 tmp
.nfs
.fs_size
= HOST_TO_LE32(fs_image
.out_size
);
751 tmp
.nfs
.fs_csum
= HOST_TO_LE32(fs_image
.csum
);
752 res
= write_out_data(outfile
, (uint8_t *)&tmp
.nfs
,
753 sizeof(tmp
.nfs
), NULL
);
761 write_out_images(FILE *outfile
)
763 struct image_desc
*desc
;
766 res
= image_writeout(outfile
, &kernel_image
);
770 res
= image_writeout(outfile
, &fs_image
);
779 find_board(char *model
)
781 struct board_info
*ret
;
782 struct board_info
*board
;
785 for (board
= boards
; board
->model
!= NULL
; board
++){
786 if (strcasecmp(model
, board
->model
) == 0) {
797 parse_opt_board(char ch
, char *arg
)
800 DBG(1,"parsing board option: -%c %s", ch
, arg
);
803 ERR("only one board option allowed");
807 if (required_arg(ch
, arg
))
810 board
= find_board(arg
);
812 ERR("invalid/unknown board specified: %s", arg
);
816 switch (board
->header_type
) {
817 case HEADER_TYPE_CAS
:
818 header
.cas
.type
= HEADER_TYPE_CAS
;
819 header
.cas
.id
= board
->id
;
821 case HEADER_TYPE_NFS
:
822 header
.nfs
.type
= HEADER_TYPE_NFS
;
823 header
.nfs
.id
= board
->id
;
826 ERR("internal error, unknown header type\n");
835 parse_opt_image(char ch
, char *arg
)
837 char buf
[MAX_ARG_LEN
];
838 char *argv
[MAX_ARG_COUNT
];
841 struct image_desc
*desc
= NULL
;
846 if (kernel_image
.file_name
) {
847 WARN("only one kernel option allowed");
850 desc
= &kernel_image
;
853 if (fs_image
.file_name
) {
854 WARN("only one fs option allowed");
864 argc
= parse_arg(arg
, buf
, argv
);
868 if (!is_empty_arg(p
)) {
869 desc
->file_name
= strdup(p
);
870 if (desc
->file_name
== NULL
) {
871 ERR("not enough memory");
875 ERR("no file specified for option %c", ch
);
886 struct image_desc
*desc
;
891 kernel_image
.out_size
= board
->max_kernel_size
;
892 kernel_image
.padc
= DEFAULT_PADC
;
893 res
= image_stat_file(&kernel_image
);
897 if (!fs_image
.file_name
)
900 fs_image
.out_size
= board
->max_fs_size
;
901 fs_image
.padc
= DEFAULT_PADC
;
902 res
= image_stat_file(&fs_image
);
911 main(int argc
, char *argv
[])
913 int optinvalid
= 0; /* flag for invalid option */
919 progname
=basename(argv
[0]);
921 opterr
= 0; /* could not print standard getopt error messages */
925 c
= getopt(argc
, argv
, "B:C:dhK:r:vw:x:");
931 optinvalid
= parse_opt_board(c
,optarg
);
934 invalid_causes_error
= 0;
938 optinvalid
= parse_opt_image(c
,optarg
);
941 keep_invalid_images
= 1;
953 if (optinvalid
!= 0 ){
954 ERR("invalid option: -%c", optopt
);
960 ERR("no board specified");
964 if (optind
== argc
) {
965 ERR("no output file specified");
969 ofname
= argv
[optind
++];
972 ERR("invalid option: %s", argv
[optind
]);
976 res
= process_images();
977 if (res
== ERR_FATAL
)
980 if (res
== ERR_INVALID_IMAGE
) {
981 if (invalid_causes_error
)
984 if (keep_invalid_images
== 0) {
985 WARN("generation of invalid images disabled", ofname
);
989 WARN("generating invalid image", ofname
);
992 outfile
= fopen(ofname
, "w");
993 if (outfile
== NULL
) {
994 ERRS("could not open \"%s\" for writing", ofname
);
999 if (write_out_header(outfile
) != 0) {
1004 if (write_out_images(outfile
) != 0) {
1009 if (write_out_header(outfile
) != 0) {
1014 DBG(1,"Image file %s completed.", ofname
);
1019 if (res
== ERR_FATAL
) {
1023 if (res
== ERR_FATAL
)
1024 return EXIT_FAILURE
;
1026 return EXIT_SUCCESS
;
This page took 0.099979 seconds and 5 git commands to generate.