wok view syslinux/stuff/extra/md5sum.c @ rev 12293

linux: fix linux64 config file
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Apr 16 09:30:43 2012 +0200 (2012-04-16)
parents 02a3222651d5
children 591795b60a3e
line source
1 /*
2 * Based on busybox code.
3 *
4 * Compute MD5 checksum of strings according to the
5 * definition of MD5 in RFC 1321 from April 1992.
6 *
7 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
8 *
9 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
10 * Copyright (C) 2001 Manuel Novoa III
11 * Copyright (C) 2003 Glenn L. McGrath
12 * Copyright (C) 2003 Erik Andersen
13 * Copyright (C) 2010 Denys Vlasenko
14 * Copyright (C) 2012 Pascal Bellard
15 *
16 * Licensed under GPLv2 or later
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <console.h>
25 #include <com32.h>
27 #define ALIGN1
29 static uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
30 static uint64_t total64; /* must be directly before hash[] */
31 static uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
33 /* Emit a string of hex representation of bytes */
34 static char* bin2hex(char *p)
35 {
36 static const char bb_hexdigits_upcase[] ALIGN1 = "0123456789abcdef";
37 int count = 16;
38 const char *cp = (const char *) hash;
39 while (count) {
40 unsigned char c = *cp++;
41 /* put lowercase hex digits */
42 *p++ = bb_hexdigits_upcase[c >> 4];
43 *p++ = bb_hexdigits_upcase[c & 0xf];
44 count--;
45 }
46 return p;
47 }
49 //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
50 static uint32_t rotl32(uint32_t x, unsigned n)
51 {
52 return (x << n) | (x >> (32 - n));
53 }
55 static void md5_process_block64(void);
57 /* Feed data through a temporary buffer.
58 * The internal buffer remembers previous data until it has 64
59 * bytes worth to pass on.
60 */
61 static void common64_hash(const void *buffer, size_t len)
62 {
63 unsigned bufpos = total64 & 63;
65 total64 += len;
67 while (1) {
68 unsigned remaining = 64 - bufpos;
69 if (remaining > len)
70 remaining = len;
71 /* Copy data into aligned buffer */
72 memcpy(wbuffer + bufpos, buffer, remaining);
73 len -= remaining;
74 buffer = (const char *)buffer + remaining;
75 bufpos += remaining;
76 /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
77 bufpos -= 64;
78 if (bufpos != 0)
79 break;
80 /* Buffer is filled up, process it */
81 md5_process_block64();
82 /*bufpos = 0; - already is */
83 }
84 }
86 /* Process the remaining bytes in the buffer */
87 static void common64_end(void)
88 {
89 unsigned bufpos = total64 & 63;
90 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
91 wbuffer[bufpos++] = 0x80;
93 /* This loop iterates either once or twice, no more, no less */
94 while (1) {
95 unsigned remaining = 64 - bufpos;
96 memset(wbuffer + bufpos, 0, remaining);
97 /* Do we have enough space for the length count? */
98 if (remaining >= 8) {
99 /* Store the 64-bit counter of bits in the buffer */
100 uint64_t t = total64 << 3;
101 /* wbuffer is suitably aligned for this */
102 *(uint64_t *) (&wbuffer[64 - 8]) = t;
103 }
104 md5_process_block64();
105 if (remaining >= 8)
106 break;
107 bufpos = 0;
108 }
109 }
111 /* These are the four functions used in the four steps of the MD5 algorithm
112 * and defined in the RFC 1321. The first function is a little bit optimized
113 * (as found in Colin Plumbs public domain implementation).
114 * #define FF(b, c, d) ((b & c) | (~b & d))
115 */
116 #undef FF
117 #undef FG
118 #undef FH
119 #undef FI
120 #define FF(b, c, d) (d ^ (b & (c ^ d)))
121 #define FG(b, c, d) FF(d, b, c)
122 #define FH(b, c, d) (b ^ c ^ d)
123 #define FI(b, c, d) (c ^ (b | ~d))
125 /* Hash a single block, 64 bytes long and 4-byte aligned */
126 static void md5_process_block64(void)
127 {
128 /* Before we start, one word to the strange constants.
129 They are defined in RFC 1321 as
130 T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
131 */
132 static const uint32_t C_array[] = {
133 /* round 1 */
134 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
135 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
136 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
137 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
138 /* round 2 */
139 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
140 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
141 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
142 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
143 /* round 3 */
144 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
145 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
146 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
147 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
148 /* round 4 */
149 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
150 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
151 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
152 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
153 };
154 static const char P_array[] ALIGN1 = {
155 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
156 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
157 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
158 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
159 };
160 uint32_t *words = (void*) wbuffer;
161 uint32_t A = hash[0];
162 uint32_t B = hash[1];
163 uint32_t C = hash[2];
164 uint32_t D = hash[3];
166 static const char S_array[] ALIGN1 = {
167 7, 12, 17, 22,
168 5, 9, 14, 20,
169 4, 11, 16, 23,
170 6, 10, 15, 21
171 };
172 const uint32_t *pc;
173 const char *pp;
174 const char *ps;
175 int i;
176 uint32_t temp;
179 pc = C_array;
180 pp = P_array;
181 ps = S_array - 4;
183 for (i = 0; i < 64; i++) {
184 if ((i & 0x0f) == 0)
185 ps += 4;
186 temp = A;
187 switch (i >> 4) {
188 case 0:
189 temp += FF(B, C, D);
190 break;
191 case 1:
192 temp += FG(B, C, D);
193 break;
194 case 2:
195 temp += FH(B, C, D);
196 break;
197 case 3:
198 temp += FI(B, C, D);
199 }
200 temp += words[(int) (*pp++)] + *pc++;
201 temp = rotl32(temp, ps[i & 3]);
202 temp += B;
203 A = D;
204 D = C;
205 C = B;
206 B = temp;
207 }
208 /* Add checksum to the starting values */
209 hash[0] += A;
210 hash[1] += B;
211 hash[2] += C;
212 hash[3] += D;
214 }
215 #undef FF
216 #undef FG
217 #undef FH
218 #undef FI
220 /* Initialize structure containing state of computation.
221 * (RFC 1321, 3.3: Step 3)
222 */
223 static void md5_begin(void)
224 {
225 hash[0] = 0x67452301;
226 hash[1] = 0xefcdab89;
227 hash[2] = 0x98badcfe;
228 hash[3] = 0x10325476;
229 total64 = 0;
230 }
232 /* Used also for sha1 and sha256 */
233 #define md5_hash common64_hash
235 /* Process the remaining bytes in the buffer and put result from CTX
236 * in first 16 bytes following RESBUF. The result is always in little
237 * endian byte order, so that a byte-wise output yields to the wanted
238 * ASCII representation of the message digest.
239 */
240 #define md5_end common64_end
242 /*
243 * Copyright (C) 2003 Glenn L. McGrath
244 * Copyright (C) 2003-2004 Erik Andersen
245 *
246 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
247 */
249 static char *unrockridge(const char *name)
250 {
251 static char buffer[256];
252 int i = 0, j = 8;
253 while (*name && i < 255) {
254 char c = *name++;
255 //if (c == '\\') c = '/';
256 if (c == '.') {
257 for (j = i; --j >= 0 && buffer[j] != '/';)
258 if (buffer[j] == '.') buffer[j] = '_';
259 if (i - j > 9) i = j + 9;
260 j = i + 4;
261 }
262 else if (c == '/') j = i + 9;
263 else if (i >= j) continue;
264 else if (c >= 'a' && c <= 'z') c += 'A' - 'a';
265 else if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) c = '_';
266 buffer[i++] = c;
267 }
268 buffer[i] = 0;
269 return buffer;
270 }
272 static uint8_t *hash_file(const char *filename)
273 {
274 int src_fd, count;
275 uint8_t in_buf[4096];
276 static uint8_t hash_value[16*2+1];
278 src_fd = open(filename, O_RDONLY);
279 if (src_fd < 0) {
280 src_fd = open(unrockridge(filename), O_RDONLY);
281 }
282 if (src_fd < 0) {
283 return NULL;
284 }
286 md5_begin();
287 while ((count = read(src_fd, in_buf, 4096)) > 0) {
288 md5_hash(in_buf, count);
289 }
291 close(src_fd);
293 if (count)
294 return NULL;
296 md5_end();
297 bin2hex((char *)hash_value);
299 return hash_value;
300 }
302 static int main_md5sum(int argc, char **argv)
303 {
304 int files = 0, tested = 0, good = 0;
305 static char clear_eol[] = " ";
307 (void) argc;
308 /* -c implied */
309 argv++;
310 do {
311 FILE *fp;
312 char eol, *line, buffer[256];
313 fp = fopen(*argv,"r");
314 if (fp == NULL)
315 fp = fopen(unrockridge(*argv),"r");
317 while ((line = fgets(buffer,256,fp)) != NULL) {
318 uint8_t *hash_value;
319 char *filename_ptr, *status;
320 int len = strlen(line);
322 if (line[0] < '0')
323 continue;
324 if (line[len-1] == '\n')
325 line[len-1] = 0;
326 filename_ptr = strstr(line, " ");
327 /* handle format for binary checksums */
328 if (filename_ptr == NULL) {
329 filename_ptr = strstr(line, " *");
330 }
331 if (filename_ptr == NULL) {
332 continue;
333 }
334 *filename_ptr = '\0';
335 *++filename_ptr = '/';
336 if (filename_ptr[1] == '/')
337 filename_ptr++;
339 files++;
340 status = "NOT CHECKED";
341 eol = '\n';
342 hash_value = hash_file(filename_ptr);
343 if (hash_value) {
344 tested++;
345 status = "BROKEN";
346 if (!strcmp((char*)hash_value, line)) {
347 good++;
348 status = "OK";
349 eol = ' ';
350 }
351 }
352 printf("\r%s: %s%s%c", filename_ptr, status, clear_eol, eol);
353 }
354 fclose(fp);
355 } while (*++argv);
356 printf("\r%d files OK, %d broken, %d not checked.%s\n",
357 good, tested - good, files - tested, clear_eol);
358 sleep(5);
359 return 0;
360 }
362 /*
363 * ifmem.c
364 *
365 * Run one command if the memory is large enought, and another if it isn't.
366 *
367 * Usage:
368 *
369 * label boot_kernel
370 * kernel ifmem.c
371 * append size_in_KB boot_large [size_in_KB boot_medium] boot_small
372 *
373 * label boot_large
374 * kernel vmlinuz_large_memory
375 * append ...
376 *
377 * label boot_small
378 * kernel vmlinuz_small_memory
379 * append ...
380 */
382 #include <inttypes.h>
383 #include <alloca.h>
384 #include <syslinux/boot.h>
386 struct e820_data {
387 uint64_t base;
388 uint64_t len;
389 uint32_t type;
390 uint32_t extattr;
391 } __attribute__((packed));
393 // Get memory size in Kb
394 static unsigned long memory_size(void)
395 {
396 uint64_t bytes = 0;
397 static com32sys_t ireg, oreg;
398 static struct e820_data ed;
400 ireg.eax.w[0] = 0xe820;
401 ireg.edx.l = 0x534d4150;
402 ireg.ecx.l = sizeof(struct e820_data);
403 ireg.edi.w[0] = OFFS(__com32.cs_bounce);
404 ireg.es = SEG(__com32.cs_bounce);
406 ed.extattr = 1;
408 do {
409 memcpy(__com32.cs_bounce, &ed, sizeof ed);
411 __intcall(0x15, &ireg, &oreg);
412 if (oreg.eflags.l & EFLAGS_CF ||
413 oreg.eax.l != 0x534d4150 ||
414 oreg.ecx.l < 20)
415 break;
417 memcpy(&ed, __com32.cs_bounce, sizeof ed);
419 if (ed.type == 1)
420 bytes += ed.len;
422 ireg.ebx.l = oreg.ebx.l;
423 } while (ireg.ebx.l);
425 if (!bytes) {
426 memset(&ireg, 0, sizeof ireg);
427 ireg.eax.w[0] = 0x8800;
428 __intcall(0x15, &ireg, &oreg);
429 return ireg.eax.w[0];
430 }
431 return bytes >> 10;
432 }
434 static int main_ifmem(int argc, char *argv[])
435 {
436 int i;
437 unsigned long ram_size;
439 if (argc < 4) {
440 perror("\nUsage: ifmem.c32 size_KB boot_large_memory boot_small_memory\n");
441 return 1;
442 }
444 // find target according to ram size
445 ram_size = memory_size();
446 printf("Total memory found %luK.\n",ram_size);
447 ram_size += (1 << 10); // add 1M to round boundaries...
449 i = 1;
450 do {
451 char *s = argv[i];
452 char *p = s;
453 unsigned long scale = 1;
455 while (*p >= '0' && *p <= '9') p++;
456 switch (*p | 0x20) {
457 case 'g': scale <<= 10;
458 case 'm': scale <<= 10;
459 default : *p = 0; break;
460 }
461 i++; // seek to label
462 if (ram_size >= scale * strtoul(s, NULL, 0)) break;
463 i++; // next size or default label
464 } while (i + 1 < argc);
466 if (i != argc) syslinux_run_command(argv[i]);
467 else syslinux_run_default();
468 return -1;
469 }
471 #include <syslinux/reboot.h>
473 static int main_reboot(int argc, char *argv[])
474 {
475 int warm = 0;
476 int i;
478 for (i = 1; i < argc; i++) {
479 if (!strcmp(argv[i], "-w") || !strcmp(argv[i], "--warm"))
480 warm = 1;
481 }
483 syslinux_reboot(warm);
484 }
486 /* APM poweroff module.
487 * based on poweroff.asm, Copyright 2009 Sebastian Herbszt
488 */
490 static int main_poweroff(int argc, char *argv[])
491 {
492 static com32sys_t ireg, oreg;
493 static char notsupported[] ="APM 1.1+ not supported";
494 unsigned i;
495 static struct {
496 unsigned short ax;
497 unsigned short bx;
498 unsigned short cx;
499 char *msg;
500 } inst[] = {
501 { 0x5300, // APM Installation Check (00h)
502 0, // APM BIOS (0000h)
503 0, "APM not present" },
504 { 0x5301, // APM Real Mode Interface Connect (01h)
505 0, // APM BIOS (0000h)
506 0, "APM RM interface connect failed" },
507 { 0x530E, // APM Driver Version (0Eh)
508 0, // APM BIOS (0000h)
509 0x0101, // APM Driver Version version 1.1
510 notsupported },
511 { 0x5307, // Set Power State (07h)
512 1, // All devices power managed by the APM
513 3, // Power state off
514 "Power off failed" }
515 };
517 (void) argc;
518 (void) argv;
519 for (i = 0; i < sizeof(inst)/sizeof(inst[0]); i++) {
520 char *msg = inst[i].msg;
522 ireg.eax.w[0] = inst[i].ax;
523 ireg.ebx.w[0] = inst[i].bx;
524 ireg.ecx.w[0] = inst[i].cx;
525 __intcall(0x15, &ireg, &oreg);
526 if ((oreg.eflags.l & EFLAGS_CF) == 0) {
527 switch (inst[i].ax) {
528 case 0x5300 :
529 if (oreg.ebx.w[0] != 0x504D /* 'PM' */) break;
530 msg = "Power management disabled";
531 if (oreg.ecx.w[0] & 8) break; // bit 3 APM BIOS Power Management disabled
532 case 0x530E :
533 msg = notsupported;
534 if (oreg.eax.w[0] < 0x101) break;
535 default : continue;
536 }
537 }
538 printf("%s.\n", msg);
539 return 1;
540 }
541 return 0;
542 }
544 /*
545 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
546 */
548 #include <syslinux/keyboard.h>
549 #include <syslinux/loadfile.h>
550 #include <syslinux/adv.h>
552 static int main_kbdmap(int argc, char *argv[])
553 {
554 const struct syslinux_keyboard_map *const kmap = syslinux_keyboard_map();
555 size_t map_size, size, i;
556 char *kbdmap, *msg;
558 msg = "Usage: kbdmap archive.cpio mapfile [cmdline]..";
559 if (argc < 3)
560 goto kbdmap_error;
562 // Save extra cmdline arguments
563 for (i = 3; i < (size_t) argc; i++) {
564 syslinux_setadv(i - 2, strlen(argv[i]), argv[i]);
565 }
567 msg = "Load error";
568 if (kmap->version != 1 ||
569 loadfile(argv[1], (void **) &kbdmap, &map_size) ||
570 strncmp(kbdmap, "07070", 5))
571 goto kbdmap_error;
573 // search for mapfile in cpio archive
574 for (i = 0; i < map_size;) {
575 int len, j;
576 char *name;
578 for (j = size = 0; j < 8; j++) {
579 char c = kbdmap[54 + i + j] - '0';
580 if (c > 9) c += '0' + 10 - 'A';
581 size <<= 4;
582 size += c;
583 }
584 i += 110;
585 name = kbdmap + i;
586 len = 1 + strlen(name);
587 i += len;
588 i += ((-i)&3);
589 if (!strcmp(name, argv[2])) {
590 kbdmap += i;
591 break;
592 }
593 i += size + ((-size)&3);
594 }
596 msg = "Filename error";
597 if (i >= map_size)
598 goto kbdmap_error;
600 msg = "Format error";
601 if (size != kmap->length)
602 goto kbdmap_error;
604 memcpy(kmap->map, kbdmap, size);
606 return 0;
608 kbdmap_error:
609 printf("%s.\n",msg);
610 return 1;
611 }
613 /* ----------------------------------------------------------------------- *
614 *
615 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
616 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
617 *
618 * Permission is hereby granted, free of charge, to any person
619 * obtaining a copy of this software and associated documentation
620 * files (the "Software"), to deal in the Software without
621 * restriction, including without limitation the rights to use,
622 * copy, modify, merge, publish, distribute, sublicense, and/or
623 * sell copies of the Software, and to permit persons to whom
624 * the Software is furnished to do so, subject to the following
625 * conditions:
626 *
627 * The above copyright notice and this permission notice shall
628 * be included in all copies or substantial portions of the Software.
629 *
630 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
631 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
632 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
633 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
634 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
635 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
636 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
637 * OTHER DEALINGS IN THE SOFTWARE.
638 *
639 * ----------------------------------------------------------------------- */
641 /*
642 * linux.c
643 *
644 * Sample module to load Linux kernels. This module can also create
645 * a file out of the DHCP return data if running under PXELINUX.
646 *
647 * If -dhcpinfo is specified, the DHCP info is written into the file
648 * /dhcpinfo.dat in the initramfs.
649 *
650 * Usage: linux.c32 [-dhcpinfo] kernel arguments...
651 */
653 #include <stdbool.h>
654 #include <stdlib.h>
655 #include <stdio.h>
656 #include <string.h>
657 #include <console.h>
658 #include <syslinux/loadfile.h>
659 #include <syslinux/linux.h>
660 #include <syslinux/pxe.h>
662 const char *progname = "linux.c32";
664 /* Find the last instance of a particular command line argument
665 (which should include the final =; do not use for boolean arguments) */
666 static char *find_argument(char **argv, const char *argument)
667 {
668 int la = strlen(argument);
669 char **arg;
670 char *ptr = NULL;
672 for (arg = argv; *arg; arg++) {
673 if (!memcmp(*arg, argument, la))
674 ptr = *arg + la;
675 }
677 return ptr;
678 }
680 /* Search for a boolean argument; return its position, or 0 if not present */
681 static int find_boolean(char **argv, const char *argument)
682 {
683 char **arg;
685 for (arg = argv; *arg; arg++) {
686 if (!strcmp(*arg, argument))
687 return (arg - argv) + 1;
688 }
690 return 0;
691 }
693 /* Stitch together the command line from a set of argv's */
694 static char *make_cmdline(char **argv)
695 {
696 char **arg;
697 size_t bytes, size;
698 char *cmdline, *p;
699 int i;
701 bytes = 1; /* Just in case we have a zero-entry cmdline */
702 for (arg = argv; *arg; arg++) {
703 bytes += strlen(*arg) + 1;
704 }
705 for (i = 0; i < 255; i++)
706 if (syslinux_getadv(i, &size))
707 bytes += ++size;
709 p = cmdline = malloc(bytes);
710 if (!cmdline)
711 return NULL;
713 for (arg = argv; *arg; arg++) {
714 int len = strlen(*arg);
715 memcpy(p, *arg, len);
716 p[len] = ' ';
717 p += len + 1;
718 }
720 for (i = 0; i < 255; i++) {
721 const void *q = syslinux_getadv(i, &size);
722 if (q == NULL) continue;
723 memcpy(p, q, size);
724 p[size] = ' ';
725 p += size + 1;
726 }
728 if (p > cmdline)
729 p--; /* Remove the last space */
730 *p = '\0';
732 return cmdline;
733 }
735 static int main_linux(int argc, char *argv[])
736 {
737 const char *kernel_name;
738 struct initramfs *initramfs;
739 char *cmdline;
740 char *boot_image;
741 void *kernel_data;
742 size_t kernel_len;
743 bool opt_dhcpinfo = false;
744 bool opt_quiet = false;
745 void *dhcpdata;
746 size_t dhcplen;
747 char **argp, *arg, *p;
749 openconsole(&dev_null_r, &dev_stdcon_w);
751 (void)argc;
752 argp = argv + 1;
754 while ((arg = *argp) && arg[0] == '-') {
755 if (!strcmp("-dhcpinfo", arg)) {
756 opt_dhcpinfo = true;
757 } else {
758 fprintf(stderr, "%s: unknown option: %s\n", progname, arg);
759 return 1;
760 }
761 argp++;
762 }
764 if (!arg) {
765 fprintf(stderr, "%s: missing kernel name\n", progname);
766 return 1;
767 }
769 kernel_name = arg;
771 boot_image = malloc(strlen(kernel_name) + 12);
772 if (!boot_image)
773 goto bail;
774 strcpy(boot_image, "BOOT_IMAGE=");
775 strcpy(boot_image + 11, kernel_name);
776 /* argp now points to the kernel name, and the command line follows.
777 Overwrite the kernel name with the BOOT_IMAGE= argument, and thus
778 we have the final argument. */
779 *argp = boot_image;
781 if (find_boolean(argp, "quiet"))
782 opt_quiet = true;
784 if (!opt_quiet)
785 printf("Loading %s... ", kernel_name);
786 if (loadfile(kernel_name, &kernel_data, &kernel_len)) {
787 if (opt_quiet)
788 printf("Loading %s ", kernel_name);
789 printf("failed!\n");
790 goto bail;
791 }
792 if (!opt_quiet)
793 printf("ok\n");
795 cmdline = make_cmdline(argp);
796 if (!cmdline)
797 goto bail;
799 /* Initialize the initramfs chain */
800 initramfs = initramfs_init();
801 if (!initramfs)
802 goto bail;
804 if ((arg = find_argument(argp, "initrd="))) {
805 do {
806 p = strchr(arg, ',');
807 if (p)
808 *p = '\0';
810 if (!opt_quiet)
811 printf("Loading %s... ", arg);
812 if (initramfs_load_archive(initramfs, arg)) {
813 if (opt_quiet)
814 printf("Loading %s ", kernel_name);
815 printf("failed!\n");
816 goto bail;
817 }
818 if (!opt_quiet)
819 printf("ok\n");
821 if (p)
822 *p++ = ',';
823 } while ((arg = p));
824 }
826 /* Append the DHCP info */
827 if (opt_dhcpinfo &&
828 !pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
829 if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
830 "/dhcpinfo.dat", 0, 0755))
831 goto bail;
832 }
834 /* This should not return... */
835 syslinux_boot_linux(kernel_data, kernel_len, initramfs, cmdline);
837 bail:
838 fprintf(stderr, "Kernel load failure (insufficient memory?)\n");
839 return 1;
840 }
842 int main(int argc, char *argv[])
843 {
844 unsigned i;
845 static struct {
846 char *name;
847 int (*main)(int argc, char *argv[]);
848 } bin[] = {
849 { "md5sum", main_md5sum },
850 { "ifmem", main_ifmem },
851 { "reboot", main_reboot },
852 { "poweroff", main_poweroff },
853 { "kbdmap", main_kbdmap },
854 { "linux", main_linux }
855 };
857 openconsole(&dev_null_r, &dev_stdcon_w);
859 if (strstr(argv[0], "c32box")) { argc--; argv++; }
860 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
861 if (strstr(argv[0], bin[i].name))
862 return bin[i].main(argc, argv);
863 printf("No %s in c32box modules\n", argv[0]);
864 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
865 printf(" %s \n",bin[i].name);
866 return 1;
867 }