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 */
151 #define MODEL_WL_5420CAM 0x020B0101 /* CAS-630W? */
152 #define MODEL_WL_5460CAM 0x020B0001 /* CAS-670W */
154 /* Repotec products */
156 /* Sitecom products */
157 #define MODEL_LN_350 /* unknown */
158 #define MODEL_LN_403 0x01020402
159 #define MODEL_WL_401 0x01010402
161 /* Surecom products */
162 #define MODEL_EP_4001_MM 0x01030A02 /* CAS-630 */
163 #define MODEL_EP_4002_MM 0x01020A02 /* CAS-630W */
164 #define MODEL_EP_4011_MM 0x01010A02 /* CAS-670 */
165 #define MODEL_EP_4012_MM 0x01000A02 /* CAS-670W */
166 #define MODEL_EP_9812_U /* unknown */
168 /* Trendnet products */
169 #define MODEL_TN_U100 0x01000081 /* NFS-101U */
170 #define MODEL_TN_U200 0x01000082 /* NFS-202U */
178 int keep_invalid_images
;
179 int invalid_causes_error
= 1;
180 union file_hdr header
;
182 struct image_desc kernel_image
;
183 struct image_desc fs_image
;
185 struct board_info
*board
= NULL
;
187 #define BOARD(m, n, i, ks, fs, h) { \
191 .max_kernel_size = (ks), \
192 .max_fs_size = (fs), \
196 #define BOARD_CAS(m,n,i) \
197 BOARD(m, n, i, KERNEL_SIZE_CAS, 0, HEADER_TYPE_CAS)
198 #define BOARD_NFS(m,n,i) \
199 BOARD(m, n, i, KERNEL_SIZE_NFS, FS_SIZE_NFS, HEADER_TYPE_NFS)
201 static struct board_info boards
[] = {
202 /* Cellvision/Sparklan products */
203 BOARD_CAS("CAS-630", "Cellvision CAS-630", MODEL_CAS_630
),
204 BOARD_CAS("CAS-630W", "Cellvision CAS-630W", MODEL_CAS_630W
),
205 BOARD_CAS("CAS-670", "Cellvision CAS-670", MODEL_CAS_670
),
206 BOARD_CAS("CAS-670W", "Cellvision CAS-670W", MODEL_CAS_670W
),
207 BOARD_NFS("NFS-101U", "Cellvision NFS-101U", MODEL_NFS_101U
),
208 BOARD_NFS("NFS-101WU", "Cellvision NFS-101WU", MODEL_NFS_101WU
),
209 BOARD_NFS("NFS-202U", "Cellvision NFS-202U", MODEL_NFS_202U
),
210 BOARD_NFS("NFS-202WU", "Cellvision NFS-202WU", MODEL_NFS_202WU
),
212 /* Corega products */
213 BOARD_NFS("CG-NSADP", "Corega CG-NSADP", MODEL_CG_NSADP
),
214 BOARD_NFS("CG-NSADPCR", "Corega CG-NSADPCR", MODEL_CG_NSADPCR
),
216 /* D-Link products */
217 BOARD_CAS("DCS-950", "D-Link DCS-950", MODEL_DCS_950
),
218 BOARD_CAS("DCS-950G", "D-Link DCS-950G", MODEL_DCS_950G
),
219 BOARD_NFS("DNS-120", "D-Link DNS-120", MODEL_DNS_120
),
220 BOARD_NFS("DNS-G120", "D-Link DNS-G120", MODEL_DNS_G120
),
222 /* Digitus products */
223 BOARD_NFS("DN-7013", "Digitus DN-7013", MODEL_DN_7013
),
226 BOARD_NFS("LB-SS01TXU", "Lobos LB-SS01TXU", MODEL_LB_SS01TXU
),
228 /* Ovislink products */
229 BOARD_NFS("MU-5000FS", "Ovislink MU-5000FS", MODEL_MU_5000FS
),
230 BOARD_CAS("WL-5420CAM", "Ovislink WL-5420 CAM", MODEL_WL_5420CAM
),
231 BOARD_CAS("WL-5460CAM", "Ovislink WL-5460 CAM", MODEL_WL_5460CAM
),
233 /* Sitecom products */
234 BOARD_CAS("LN-403", "Sitecom LN-403", MODEL_LN_403
),
235 BOARD_CAS("WL-401", "Sitecom WL-401", MODEL_WL_401
),
237 /* Surecom products */
238 BOARD_CAS("EP-4001-MM", "Surecom EP-4001-MM", MODEL_EP_4001_MM
),
239 BOARD_CAS("EP-4002-MM", "Surecom EP-4002-MM", MODEL_EP_4002_MM
),
240 BOARD_CAS("EP-4011-MM", "Surecom EP-4011-MM", MODEL_EP_4011_MM
),
241 BOARD_CAS("EP-4012-MM", "Surecom EP-4012-MM", MODEL_EP_4012_MM
),
243 /* TrendNET products */
244 BOARD_NFS("TN-U100", "TrendNET TN-U100", MODEL_TN_U100
),
245 BOARD_NFS("TN-U200", "TrendNET TN-U200", MODEL_TN_U200
),
253 #define ERR(fmt, ...) do { \
255 fprintf(stderr, "[%s] *** error: " fmt "\n", \
256 progname, ## __VA_ARGS__ ); \
259 #define ERRS(fmt, ...) do { \
262 fprintf(stderr, "[%s] *** error: " fmt "\n", \
263 progname, ## __VA_ARGS__, strerror(save)); \
266 #define WARN(fmt, ...) do { \
267 fprintf(stderr, "[%s] *** warning: " fmt "\n", \
268 progname, ## __VA_ARGS__ ); \
271 #define DBG(lev, fmt, ...) do { \
272 if (verblevel < lev) \
274 fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
278 #define ERR_INVALID_IMAGE -2
283 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
284 struct board_info
*board
;
286 fprintf(stream
, "Usage: %s [OPTIONS...] <file>\n", progname
);
290 " -B <board> create image for the board specified with <board>.\n"
291 " valid <board> values:\n"
293 for (board
= boards
; board
->model
!= NULL
; board
++){
296 board
->model
, board
->name
);
299 " -d don't throw error on invalid images\n"
300 " -k keep invalid images\n"
301 " -K <file> add kernel to the image\n"
302 " -C <file> add custom filesystem to the image\n"
303 " -h show this screen\n"
305 " <file> write output to the file <file>\n"
311 static inline uint32_t align(uint32_t base
, uint32_t alignment
)
316 ret
= (base
+ alignment
- 1);
317 ret
&= ~(alignment
-1);
329 str2u32(char *arg
, uint32_t *val
)
335 t
= strtoul(arg
, &err
, 0);
336 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
)) {
346 str2u16(char *arg
, uint16_t *val
)
352 t
= strtoul(arg
, &err
, 0);
353 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x10000)) {
362 str2u8(char *arg
, uint8_t *val
)
368 t
= strtoul(arg
, &err
, 0);
369 if (errno
|| (err
==arg
) || ((err
!= NULL
) && *err
) || (t
>= 0x100)) {
378 parse_arg(char *arg
, char *buf
, char *argv
[])
386 memset(argv
, 0, MAX_ARG_COUNT
* sizeof(void *));
399 if (argl
>= MAX_ARG_LEN
) {
400 /* argument is too long */
401 argl
= MAX_ARG_LEN
-1;
404 memcpy(buf
, arg
, argl
);
407 for (i
= 0; i
< MAX_ARG_COUNT
; i
++) {
408 tok
= strsep(ap
, ":");
413 else if (tok
[0] == '\0') {
426 required_arg(char c
, char *arg
)
428 if (arg
== NULL
|| *arg
!= '-')
431 ERR("option -%c requires an argument\n", c
);
437 is_empty_arg(char *arg
)
448 csum8_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
450 for ( ; len
> 0; len
--) {
457 csum8_get(struct csum_state
*css
)
467 csum16_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
472 t
= css
->tmp
+ (p
[0]<<8);
473 css
->val
+= LE16_TO_HOST(t
);
479 for ( ; len
> 1; len
-= 2, p
+=2 ) {
480 t
= p
[0] + (p
[1] << 8);
481 css
->val
+= LE16_TO_HOST(t
);
492 csum16_get(struct csum_state
*css
)
496 csum16_update(&pad
, 1, css
);
497 return ~css
->val
+ 1;
501 csum32_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
505 for ( ; len
> 3; len
-= 4, p
+= 4 ) {
506 t
= p
[0] + (p
[1] << 8) + (p
[2] << 16) + (p
[3] << 24);
512 csum32_get(struct csum_state
*css
)
519 csum_init(struct csum_state
*css
, int size
)
528 csum_update(uint8_t *p
, uint32_t len
, struct csum_state
*css
)
532 csum8_update(p
,len
,css
);
535 csum16_update(p
,len
,css
);
538 csum32_update(p
,len
,css
);
545 csum_get(struct csum_state
*css
)
551 ret
= csum8_get(css
);
554 ret
= csum16_get(css
);
557 ret
= csum32_get(css
);
565 * routines to write data to the output file
568 write_out_data(FILE *outfile
, uint8_t *data
, size_t len
,
569 struct csum_state
*css
)
573 fwrite(data
, len
, 1, outfile
);
575 ERRS("unable to write output file");
580 csum_update(data
, len
, css
);
588 write_out_padding(FILE *outfile
, size_t len
, uint8_t padc
,
589 struct csum_state
*css
)
592 size_t buflen
= sizeof(buf
);
595 memset(buf
, padc
, buflen
);
600 err
= write_out_data(outfile
, buf
, buflen
, css
);
612 image_stat_file(struct image_desc
*desc
)
617 if (desc
->file_name
== NULL
)
620 err
= stat(desc
->file_name
, &st
);
622 ERRS("stat failed on %s", desc
->file_name
);
626 if (st
.st_size
> desc
->out_size
) {
627 WARN("file %s is too big, will be truncated to %d bytes\n",
628 desc
->file_name
, desc
->out_size
);
629 desc
->file_size
= desc
->out_size
;
630 return ERR_INVALID_IMAGE
;
634 desc
->file_size
= st
.st_size
;
635 desc
->out_size
= align(desc
->file_size
,1);
641 image_writeout_file(FILE *outfile
, struct image_desc
*desc
,
642 struct csum_state
*css
)
644 char buf
[FILE_BUF_LEN
];
645 size_t buflen
= sizeof(buf
);
650 if (desc
->file_name
== NULL
)
653 if (desc
->file_size
== 0)
657 f
= fopen(desc
->file_name
,"r");
659 ERRS("unable to open file: %s", desc
->file_name
);
663 len
= desc
->file_size
;
668 /* read data from source file */
670 fread(buf
, buflen
, 1, f
);
672 ERRS("unable to read from file: %s", desc
->file_name
);
677 res
= write_out_data(outfile
, buf
, buflen
, css
);
690 image_writeout(FILE *outfile
, struct image_desc
*desc
)
693 struct csum_state css
;
698 if (!desc
->file_size
)
701 DBG(2, "writing image, file=%s, file_size=%d\n",
702 desc
->file_name
, desc
->file_size
);
704 csum_init(&css
, CSUM_TYPE_32
);
706 res
= image_writeout_file(outfile
, desc
, &css
);
710 /* write padding data if neccesary */
711 padlen
= desc
->out_size
- desc
->file_size
;
712 DBG(1,"padding desc, length=%d", padlen
);
713 res
= write_out_padding(outfile
, padlen
, desc
->padc
, &css
);
715 desc
->csum
= csum_get(&css
);
722 write_out_header(FILE *outfile
)
728 if (fseek(outfile
, 0, SEEK_SET
) != 0) {
729 ERRS("fseek failed on output file");
733 switch (board
->header_type
) {
734 case HEADER_TYPE_CAS
:
735 tmp
.cas
.type
= HOST_TO_LE32(header
.cas
.type
);
736 tmp
.cas
.id
= HOST_TO_LE32(header
.cas
.id
);
737 tmp
.cas
.kernel_offs
= HOST_TO_LE32(sizeof(tmp
.cas
));
738 tmp
.cas
.kernel_size
= HOST_TO_LE32(kernel_image
.out_size
);
739 tmp
.cas
.kernel_csum
= HOST_TO_LE32(kernel_image
.csum
);
740 tmp
.cas
.magic1
= HOST_TO_LE32(CAS_MAGIC1
);
741 tmp
.cas
.magic2
= HOST_TO_LE32(CAS_MAGIC2
);
742 tmp
.cas
.magic3
= HOST_TO_LE32(CAS_MAGIC3
);
743 res
= write_out_data(outfile
, (uint8_t *)&tmp
.cas
,
744 sizeof(tmp
.cas
), NULL
);
746 case HEADER_TYPE_NFS
:
747 tmp
.nfs
.type
= HOST_TO_LE32(header
.nfs
.type
);
748 tmp
.nfs
.id
= HOST_TO_LE32(header
.nfs
.id
);
749 tmp
.nfs
.kernel_offs
= HOST_TO_LE32(sizeof(tmp
.nfs
));
750 tmp
.nfs
.kernel_size
= HOST_TO_LE32(kernel_image
.out_size
);
751 tmp
.nfs
.kernel_csum
= HOST_TO_LE32(kernel_image
.csum
);
752 tmp
.nfs
.fs_offs
= HOST_TO_LE32(sizeof(tmp
.nfs
)
753 + kernel_image
.out_size
);
754 tmp
.nfs
.fs_size
= HOST_TO_LE32(fs_image
.out_size
);
755 tmp
.nfs
.fs_csum
= HOST_TO_LE32(fs_image
.csum
);
756 res
= write_out_data(outfile
, (uint8_t *)&tmp
.nfs
,
757 sizeof(tmp
.nfs
), NULL
);
765 write_out_images(FILE *outfile
)
767 struct image_desc
*desc
;
770 res
= image_writeout(outfile
, &kernel_image
);
774 res
= image_writeout(outfile
, &fs_image
);
783 find_board(char *model
)
785 struct board_info
*ret
;
786 struct board_info
*board
;
789 for (board
= boards
; board
->model
!= NULL
; board
++){
790 if (strcasecmp(model
, board
->model
) == 0) {
801 parse_opt_board(char ch
, char *arg
)
804 DBG(1,"parsing board option: -%c %s", ch
, arg
);
807 ERR("only one board option allowed");
811 if (required_arg(ch
, arg
))
814 board
= find_board(arg
);
816 ERR("invalid/unknown board specified: %s", arg
);
820 switch (board
->header_type
) {
821 case HEADER_TYPE_CAS
:
822 header
.cas
.type
= HEADER_TYPE_CAS
;
823 header
.cas
.id
= board
->id
;
825 case HEADER_TYPE_NFS
:
826 header
.nfs
.type
= HEADER_TYPE_NFS
;
827 header
.nfs
.id
= board
->id
;
830 ERR("internal error, unknown header type\n");
839 parse_opt_image(char ch
, char *arg
)
841 char buf
[MAX_ARG_LEN
];
842 char *argv
[MAX_ARG_COUNT
];
845 struct image_desc
*desc
= NULL
;
850 if (kernel_image
.file_name
) {
851 WARN("only one kernel option allowed");
854 desc
= &kernel_image
;
857 if (fs_image
.file_name
) {
858 WARN("only one fs option allowed");
868 argc
= parse_arg(arg
, buf
, argv
);
872 if (!is_empty_arg(p
)) {
873 desc
->file_name
= strdup(p
);
874 if (desc
->file_name
== NULL
) {
875 ERR("not enough memory");
879 ERR("no file specified for option %c", ch
);
890 struct image_desc
*desc
;
895 kernel_image
.out_size
= board
->max_kernel_size
;
896 kernel_image
.padc
= DEFAULT_PADC
;
897 res
= image_stat_file(&kernel_image
);
901 if (!fs_image
.file_name
)
904 fs_image
.out_size
= board
->max_fs_size
;
905 fs_image
.padc
= DEFAULT_PADC
;
906 res
= image_stat_file(&fs_image
);
915 main(int argc
, char *argv
[])
917 int optinvalid
= 0; /* flag for invalid option */
923 progname
=basename(argv
[0]);
925 opterr
= 0; /* could not print standard getopt error messages */
929 c
= getopt(argc
, argv
, "B:C:dhK:r:vw:x:");
935 optinvalid
= parse_opt_board(c
,optarg
);
938 invalid_causes_error
= 0;
942 optinvalid
= parse_opt_image(c
,optarg
);
945 keep_invalid_images
= 1;
957 if (optinvalid
!= 0 ){
958 ERR("invalid option: -%c", optopt
);
964 ERR("no board specified");
968 if (optind
== argc
) {
969 ERR("no output file specified");
973 ofname
= argv
[optind
++];
976 ERR("invalid option: %s", argv
[optind
]);
980 res
= process_images();
981 if (res
== ERR_FATAL
)
984 if (res
== ERR_INVALID_IMAGE
) {
985 if (invalid_causes_error
)
988 if (keep_invalid_images
== 0) {
989 WARN("generation of invalid images disabled", ofname
);
993 WARN("generating invalid image", ofname
);
996 outfile
= fopen(ofname
, "w");
997 if (outfile
== NULL
) {
998 ERRS("could not open \"%s\" for writing", ofname
);
1003 if (write_out_header(outfile
) != 0) {
1008 if (write_out_images(outfile
) != 0) {
1013 if (write_out_header(outfile
) != 0) {
1018 DBG(1,"Image file %s completed.", ofname
);
1023 if (res
== ERR_FATAL
) {
1027 if (res
== ERR_FATAL
)
1028 return EXIT_FAILURE
;
1030 return EXIT_SUCCESS
;
This page took 0.100295 seconds and 5 git commands to generate.