3 * Copyright (c) 2013 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This utility sorts sysinit structure entries in binary format and
29 * prints out the result in C-format.
43 static const char *input_f;
44 static const char *output_f;
45 static const char *struct_name;
46 static const char *keyword;
47 static struct sysinit_data **start;
48 static struct sysinit_data **stop;
50 static int input_file = -1;
51 static int output_file = -1;
53 static uint8_t *input_ptr;
54 static uint32_t input_len;
56 static uint32_t endian32;
58 static char scratch_buf[4096];
62 static void do_sysinit(void);
64 /* the following function converts the numbers into host endian format */
76 temp |= (val & 0xF) << ((endian & 0xF) * 4);
84 do_write(int fd, const char *buf)
86 int len = strlen(buf);
88 if (write(fd, buf, len) != len)
89 err(EX_SOFTWARE, "Could not write to output file");
99 errx(EX_SOFTWARE, "Could not allocate memory");
106 errx(EX_USAGE, "sysinit -i sysinit.bin -o sysinit_data.c \\\n"
107 "\t" "-k sysinit -s sysinit_data [ -R (reverse)]");
113 if (output_file >= 0)
124 compare(const void *_pa, const void *_pb)
126 const struct sysinit_data * const *pa = _pa;
127 const struct sysinit_data * const *pb = _pb;
129 if ((*pa)->dw_msb_value > (*pb)->dw_msb_value)
132 if ((*pa)->dw_msb_value < (*pb)->dw_msb_value)
135 if ((*pa)->dw_lsb_value > (*pb)->dw_lsb_value)
138 if ((*pa)->dw_lsb_value < (*pb)->dw_lsb_value)
141 return (0); /* equal */
145 compare_R(const void *_pa, const void *_pb)
147 const struct sysinit_data * const *pa = _pa;
148 const struct sysinit_data * const *pb = _pb;
150 if ((*pa)->dw_msb_value > (*pb)->dw_msb_value)
153 if ((*pa)->dw_msb_value < (*pb)->dw_msb_value)
156 if ((*pa)->dw_lsb_value > (*pb)->dw_lsb_value)
159 if ((*pa)->dw_lsb_value < (*pb)->dw_lsb_value)
162 return (0); /* equal */
166 main(int argc, char **argv)
168 struct sysinit_data **sipp;
173 while ((c = getopt(argc, argv, "k:s:i:o:Rh")) != -1) {
188 struct_name = optarg;
195 if (input_f == NULL || output_f == NULL ||
196 struct_name == NULL || keyword == NULL)
203 input_file = open(input_f, O_RDONLY);
205 err(EX_SOFTWARE, "Could not open input file: %s", input_f);
207 output_file = open(output_f, O_TRUNC | O_CREAT | O_RDWR, 0600);
209 err(EX_SOFTWARE, "Could not open output file: %s", output_f);
211 off = lseek(input_file, 0, SEEK_END);
213 input_ptr = do_malloc(off);
216 if (input_len % (uint32_t)sizeof(struct sysinit_data)) {
217 errx(EX_SOFTWARE, "Input file size is not divisible by %u",
218 (unsigned int)sizeof(struct sysinit_data));
220 off = lseek(input_file, 0, SEEK_SET);
222 err(EX_SOFTWARE, "Could not seek to start of input file");
224 if (read(input_file, input_ptr, input_len) != input_len)
225 err(EX_SOFTWARE, "Could not read input file");
227 entries = input_len / (uint32_t)sizeof(struct sysinit_data);
229 start = do_malloc(sizeof(void *) * entries);
230 stop = start + entries;
232 for (c = 0; c != entries; c++)
233 start[c] = &((struct sysinit_data *)input_ptr)[c];
236 endian32 = (*start)->dw_endian32;
238 /* switch all fields to host endian order */
239 for (sipp = start; sipp < stop; sipp++) {
240 (*sipp)->dw_lsb_value = read32((*sipp)->dw_lsb_value);
241 (*sipp)->dw_msb_value = read32((*sipp)->dw_msb_value);
242 (*sipp)->dw_file_line = read32((*sipp)->dw_file_line);
246 /* sort entries, rising numerical order */
247 qsort(start, entries, sizeof(void *), &compare);
249 /* sort entries, falling numerical order */
250 qsort(start, entries, sizeof(void *), &compare_R);
253 /* safe all strings */
254 for (sipp = start; sipp < stop; sipp++) {
255 (*sipp)->b_keyword_name[sizeof((*sipp)->b_keyword_name) - 1] = 0;
256 (*sipp)->b_global_type[sizeof((*sipp)->b_global_type) - 1] = 0;
257 (*sipp)->b_global_name[sizeof((*sipp)->b_global_name) - 1] = 0;
258 (*sipp)->b_file_name[sizeof((*sipp)->b_file_name) - 1] = 0;
259 (*sipp)->b_debug_info[sizeof((*sipp)->b_debug_info) - 1] = 0;
262 if (strcmp(keyword, "sysinit") == 0)
264 else if (strcmp(keyword, "sysuninit") == 0)
267 errx(EX_USAGE, "Unknown keyword '%s'", keyword);
277 struct sysinit_data **sipp;
280 snprintf(scratch_buf, sizeof(scratch_buf),
282 " * This file was automatically generated.\n"
283 " * Please do not edit.\n"
286 /* write out externals */
287 for (c = 0, sipp = start; sipp < stop; c++, sipp++) {
288 if (strcmp((const char *)(*sipp)->b_keyword_name, keyword))
290 if ((*sipp)->dw_msb_value == 0)
293 snprintf(scratch_buf, sizeof(scratch_buf),
294 "/* #%04u: %s entry at %s:%u */\n",
295 c, (*sipp)->b_debug_info, (*sipp)->b_file_name,
296 (unsigned int)(*sipp)->dw_file_line);
298 do_write(output_file, scratch_buf);
300 snprintf(scratch_buf, sizeof(scratch_buf),
301 "extern %s %s;\n\n", (*sipp)->b_global_type,
302 (*sipp)->b_global_name);
304 do_write(output_file, scratch_buf);
307 snprintf(scratch_buf, sizeof(scratch_buf),
308 "const void *%s[] = {\n", struct_name);
310 do_write(output_file, scratch_buf);
312 /* write out actual table */
313 for (c = 0, sipp = start; sipp < stop; c++, sipp++) {
314 if (strcmp((const char *)(*sipp)->b_keyword_name, keyword))
316 if ((*sipp)->dw_msb_value == 0)
319 snprintf(scratch_buf, sizeof(scratch_buf),
320 "\t&%s, /* #%04u */\n",
321 (*sipp)->b_global_name, (unsigned int)c);
323 do_write(output_file, scratch_buf);
326 snprintf(scratch_buf, sizeof(scratch_buf),
327 "\t(const void *)0\n"
330 do_write(output_file, scratch_buf);