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

syslinux/c32box: x86_64 auto select
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Aug 21 09:56:57 2014 +0200 (2014-08-21)
parents 3f48e3a93cd7
children d5b427eca7f3
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_say(int argc, char **argv)
303 {
304 int i;
305 for (i = 1; i < argc; i++) {
306 printf("%s ",argv[i]);
307 }
308 sleep(5);
309 return 0;
310 }
312 static int main_md5sum(int argc, char **argv)
313 {
314 int files = 0, tested = 0, good = 0;
315 static char clear_eol[] = " ";
317 (void) argc;
318 /* -c implied */
319 argv++;
320 do {
321 FILE *fp;
322 char eol, *line, buffer[4096];
323 fp = fopen(*argv,"r");
324 if (fp == NULL)
325 fp = fopen(unrockridge(*argv),"r");
327 while ((line = fgets(buffer,sizeof(buffer),fp)) != NULL) {
328 uint8_t *hash_value;
329 char *filename_ptr, *status;
330 int len = strlen(line);
332 if (line[0] < '0')
333 continue;
334 if (line[len-1] == '\n')
335 line[len-1] = 0;
336 filename_ptr = strstr(line, " ");
337 /* handle format for binary checksums */
338 if (filename_ptr == NULL) {
339 filename_ptr = strstr(line, " *");
340 }
341 if (filename_ptr == NULL) {
342 continue;
343 }
344 *filename_ptr = '\0';
345 *++filename_ptr = '/';
346 if (filename_ptr[1] == '/')
347 filename_ptr++;
349 files++;
350 status = "NOT CHECKED";
351 eol = '\n';
352 hash_value = hash_file(filename_ptr);
353 if (hash_value) {
354 tested++;
355 status = "BROKEN";
356 if (!strcmp((char*)hash_value, line)) {
357 good++;
358 status = "OK";
359 eol = ' ';
360 }
361 }
362 printf("\r%s: %s%s%c", filename_ptr, status, clear_eol, eol);
363 }
364 fclose(fp);
365 } while (*++argv);
366 printf("\r%d files OK, %d broken, %d not checked.%s\n",
367 good, tested - good, files - tested, clear_eol);
368 sleep(5);
369 return 0;
370 }
372 /*
373 * ifmem.c
374 *
375 * Run one command if the memory is large enought, and another if it isn't.
376 *
377 * Usage:
378 *
379 * label boot_kernel
380 * kernel ifmem.c
381 * append size_in_KB boot_large [size_in_KB boot_medium] boot_small
382 *
383 * label boot_large
384 * kernel vmlinuz_large_memory
385 * append ...
386 *
387 * label boot_small
388 * kernel vmlinuz_small_memory
389 * append ...
390 */
392 #include <inttypes.h>
393 #include <alloca.h>
394 #include <syslinux/boot.h>
396 struct e820_data {
397 uint64_t base;
398 uint64_t len;
399 uint32_t type;
400 uint32_t extattr;
401 } __attribute__((packed));
403 // Get memory size in Kb
404 static unsigned long memory_size(void)
405 {
406 uint64_t bytes = 0;
407 static com32sys_t ireg, oreg;
408 static struct e820_data ed;
410 ireg.eax.w[0] = 0xe820;
411 ireg.edx.l = 0x534d4150;
412 ireg.ecx.l = sizeof(struct e820_data);
413 ireg.edi.w[0] = OFFS(__com32.cs_bounce);
414 ireg.es = SEG(__com32.cs_bounce);
416 ed.extattr = 1;
418 do {
419 memcpy(__com32.cs_bounce, &ed, sizeof ed);
421 __intcall(0x15, &ireg, &oreg);
422 if (oreg.eflags.l & EFLAGS_CF ||
423 oreg.eax.l != 0x534d4150 ||
424 oreg.ecx.l < 20)
425 break;
427 memcpy(&ed, __com32.cs_bounce, sizeof ed);
429 if (ed.type == 1)
430 bytes += ed.len;
432 ireg.ebx.l = oreg.ebx.l;
433 } while (ireg.ebx.l);
435 if (!bytes) {
436 memset(&ireg, 0, sizeof ireg);
437 ireg.eax.w[0] = 0x8800;
438 __intcall(0x15, &ireg, &oreg);
439 return ireg.eax.w[0];
440 }
441 return bytes >> 10;
442 }
444 static void usage(const char *msg)
445 {
446 fprintf(stderr,"\n%s\n.",msg);
447 sleep(5);
448 exit(1);
449 }
451 static int main_ifmem(int argc, char *argv[])
452 {
453 int i;
454 unsigned long ram_size;
456 if (argc < 4) {
457 usage("Usage: ifmem.c32 size_KB boot_large_memory boot_small_memory");
458 }
460 // find target according to ram size
461 ram_size = memory_size();
462 printf("Total memory found %luK.\n",ram_size);
463 ram_size += (1 << 10); // add 1M to round boundaries...
465 i = 1;
466 do {
467 char *s = argv[i];
468 char *p = s;
469 unsigned long scale = 1;
471 while (*p >= '0' && *p <= '9') p++;
472 switch (*p | 0x20) {
473 case 'g': scale <<= 10;
474 case 'm': scale <<= 10;
475 default : *p = 0; break;
476 }
477 i++; // seek to label
478 if (ram_size >= scale * strtoul(s, NULL, 0)) break;
479 i++; // next size or default label
480 } while (i + 1 < argc);
482 if (i != argc) syslinux_run_command(argv[i]);
483 else syslinux_run_default();
484 return -1;
485 }
487 #include <syslinux/reboot.h>
489 static int main_reboot(int argc, char *argv[])
490 {
491 int warm = 0;
492 int i;
494 for (i = 1; i < argc; i++) {
495 if (!strcmp(argv[i], "-w") || !strcmp(argv[i], "--warm"))
496 warm = 1;
497 }
499 syslinux_reboot(warm);
500 }
502 /* APM poweroff module.
503 * based on poweroff.asm, Copyright 2009 Sebastian Herbszt
504 */
506 static int main_poweroff(int argc, char *argv[])
507 {
508 static com32sys_t ireg, oreg;
509 static char notsupported[] ="APM 1.1+ not supported";
510 unsigned i;
511 static struct {
512 unsigned short ax;
513 unsigned short bx;
514 unsigned short cx;
515 char *msg;
516 } inst[] = {
517 { 0x5300, // APM Installation Check (00h)
518 0, // APM BIOS (0000h)
519 0, "APM not present" },
520 { 0x5301, // APM Real Mode Interface Connect (01h)
521 0, // APM BIOS (0000h)
522 0, "APM RM interface connect failed" },
523 { 0x530E, // APM Driver Version (0Eh)
524 0, // APM BIOS (0000h)
525 0x0101, // APM Driver Version version 1.1
526 notsupported },
527 { 0x5307, // Set Power State (07h)
528 1, // All devices power managed by the APM
529 3, // Power state off
530 "Power off failed" }
531 };
533 (void) argc;
534 (void) argv;
535 for (i = 0; i < sizeof(inst)/sizeof(inst[0]); i++) {
536 char *msg = inst[i].msg;
538 ireg.eax.w[0] = inst[i].ax;
539 ireg.ebx.w[0] = inst[i].bx;
540 ireg.ecx.w[0] = inst[i].cx;
541 __intcall(0x15, &ireg, &oreg);
542 if ((oreg.eflags.l & EFLAGS_CF) == 0) {
543 switch (inst[i].ax) {
544 case 0x5300 :
545 if (oreg.ebx.w[0] != 0x504D /* 'PM' */) break;
546 msg = "Power management disabled";
547 if (oreg.ecx.w[0] & 8) break; // bit 3 APM BIOS Power Management disabled
548 case 0x530E :
549 msg = notsupported;
550 if (oreg.eax.w[0] < 0x101) break;
551 default : continue;
552 }
553 }
554 printf("%s.\n", msg);
555 return 1;
556 }
557 return 0;
558 }
560 /*
561 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
562 */
564 #include <syslinux/keyboard.h>
565 #include <syslinux/loadfile.h>
566 #include <syslinux/adv.h>
568 static void setlinuxarg(int slot, int argc, char *argv[])
569 {
570 for (; argc--; argv++)
571 syslinux_setadv(slot++, strlen(*argv), *argv);
572 }
574 static int main_kbdmap(int argc, char *argv[])
575 {
576 const struct syslinux_keyboard_map *const kmap = syslinux_keyboard_map();
577 size_t map_size, size, i;
578 char *kbdmap, *msg;
580 if (argc < 3)
581 usage("Usage: kbdmap archive.cpio mapfile [cmdline]..");
583 // Save extra cmdline arguments
584 setlinuxarg(1, argc - 3, argv + 3);
586 msg="Append to kernel parameters: ";
587 for (i = 3; i < (size_t) argc; i++, msg = " ")
588 printf("%s%s",msg,argv[i]);
589 printf("\n\n Hit RETURN to continue.\n");
591 msg = "Load error";
592 if (kmap->version != 1 ||
593 loadfile(argv[1], (void **) &kbdmap, &map_size) ||
594 strncmp(kbdmap, "07070", 5))
595 goto kbdmap_error;
597 // search for mapfile in cpio archive
598 for (i = 0; i < map_size;) {
599 int len, j;
600 char *name;
602 for (j = size = 0; j < 8; j++) {
603 char c = kbdmap[54 + i + j] - '0';
604 if (c > 9) c += '0' + 10 - 'A';
605 size <<= 4;
606 size += c;
607 }
608 i += 110;
609 name = kbdmap + i;
610 len = 1 + strlen(name);
611 i += len;
612 i += ((-i)&3);
613 if (!strcmp(name, argv[2])) {
614 kbdmap += i;
615 break;
616 }
617 i += size + ((-size)&3);
618 }
620 msg = "Filename error";
621 if (i >= map_size)
622 goto kbdmap_error;
624 msg = "Format error";
625 if (size != kmap->length)
626 goto kbdmap_error;
628 memcpy(kmap->map, kbdmap, size);
630 return 0;
632 kbdmap_error:
633 printf("%s.\n",msg);
634 return 1;
635 }
637 /* ----------------------------------------------------------------------- *
638 *
639 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
640 * Copyright 2009-2012 Intel Corporation; author: H. Peter Anvin
641 *
642 * Permission is hereby granted, free of charge, to any person
643 * obtaining a copy of this software and associated documentation
644 * files (the "Software"), to deal in the Software without
645 * restriction, including without limitation the rights to use,
646 * copy, modify, merge, publish, distribute, sublicense, and/or
647 * sell copies of the Software, and to permit persons to whom
648 * the Software is furnished to do so, subject to the following
649 * conditions:
650 *
651 * The above copyright notice and this permission notice shall
652 * be included in all copies or substantial portions of the Software.
653 *
654 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
655 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
656 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
657 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
658 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
659 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
660 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
661 * OTHER DEALINGS IN THE SOFTWARE.
662 *
663 * ----------------------------------------------------------------------- */
665 /*
666 * linux.c
667 *
668 * Sample module to load Linux kernels. This module can also create
669 * a file out of the DHCP return data if running under PXELINUX.
670 *
671 * If -dhcpinfo is specified, the DHCP info is written into the file
672 * /dhcpinfo.dat in the initramfs.
673 *
674 * Usage: linux.c32 [-dhcpinfo] kernel arguments...
675 */
677 #include <errno.h>
678 #include <stdbool.h>
679 #include <stdlib.h>
680 #include <stdio.h>
681 #include <string.h>
682 #include <console.h>
683 #include <cpuid.h>
684 #include <syslinux/loadfile.h>
685 #include <syslinux/linux.h>
686 #include <syslinux/pxe.h>
688 const char *progname = "linux.c32";
690 /* Find the last instance of a particular command line argument
691 (which should include the final =; do not use for boolean arguments) */
692 static char *find_argument(char **argv, const char *argument)
693 {
694 int la = strlen(argument);
695 char **arg;
696 char *ptr = NULL;
698 for (arg = argv; *arg; arg++) {
699 if (!memcmp(*arg, argument, la))
700 ptr = *arg + la;
701 }
703 return ptr;
704 }
706 /* Search for a boolean argument; return its position, or 0 if not present */
707 static int find_boolean(char **argv, const char *argument)
708 {
709 char **arg;
711 for (arg = argv; *arg; arg++) {
712 if (!strcmp(*arg, argument))
713 return (arg - argv) + 1;
714 }
716 return 0;
717 }
719 /* Stitch together the command line from a set of argv's */
720 static char *make_cmdline(char **argv)
721 {
722 char **arg;
723 size_t bytes, size;
724 char *cmdline, *p;
725 int i;
727 bytes = 1; /* Just in case we have a zero-entry cmdline */
728 for (arg = argv; *arg; arg++) {
729 bytes += strlen(*arg) + 1;
730 }
731 for (i = 0; i < 255; i++)
732 if (syslinux_getadv(i, &size))
733 bytes += ++size;
735 p = cmdline = malloc(bytes);
736 if (!cmdline)
737 return NULL;
739 for (arg = argv; *arg; arg++) {
740 int len = strlen(*arg);
741 memcpy(p, *arg, len);
742 p[len] = ' ';
743 p += len + 1;
744 }
746 for (i = 0; i < 255; i++) {
747 const void *q = syslinux_getadv(i, &size);
748 if (q == NULL) continue;
749 memcpy(p, q, size);
750 p[size] = ' ';
751 p += size + 1;
752 }
754 if (p > cmdline)
755 p--; /* Remove the last space */
756 *p = '\0';
758 return cmdline;
759 }
761 static bool __constfunc cpu_has_cpuid(void)
762 {
763 return cpu_has_eflag(X86_EFLAGS_ID);
764 }
766 static bool __constfunc cpu_has_level(uint32_t level)
767 {
768 uint32_t group;
769 uint32_t limit;
771 if (!cpu_has_cpuid())
772 return false;
774 group = level & 0xffff0000;
775 limit = cpuid_eax(group);
777 if ((limit & 0xffff0000) != group)
778 return false;
780 if (level > limit)
781 return false;
783 return true;
784 }
786 /* This only supports feature groups 0 and 1, corresponding to the
787 Intel and AMD EDX bit vectors. We can add more later if need be. */
788 static bool __constfunc cpu_has_feature(int x)
789 {
790 uint32_t level = ((x & 1) << 31) | 1;
792 return cpu_has_level(level) && ((cpuid_edx(level) >> (x & 31) & 1));
793 }
795 static char *extfilename(char *filename, char *ext, int feature)
796 {
797 #define NEWFILENAMESZ 80
798 static char newfilename[NEWFILENAMESZ+1];
799 char *found = filename;
800 FILE *fp;
802 if (strlen(filename) + strlen(ext) <= NEWFILENAMESZ) {
803 strcpy(newfilename, filename, NEWFILENAMESZ);
804 if (cpu_has_feature(feature)) {
805 strcat(newfilename, ext);
806 fp = fopen(newfilename, "r");
807 if (fp)
808 found = newfilename;
809 fclose(fp);
810 }
811 }
812 return found;
813 }
815 static char *bestextfilename(char *filename)
816 {
817 char *found;
819 //found = extfilename(filename, "fpu", X86_FEATURE_FPU);
820 //found = extfilename(filename, "686", X86_FEATURE_CMOV);
821 //found = extfilename(filename, "pae", X86_FEATURE_PAE);
822 found = extfilename(filename, "64", X86_FEATURE_LM);
823 //found = extfilename(filename, "guest", X86_FEATURE_HYPERVISOR);
824 return found;
825 }
827 static int setup_data_file(struct setup_data *setup_data,
828 uint32_t type, const char *filename,
829 bool opt_quiet)
830 {
831 if (!opt_quiet)
832 printf("Loading %s... ", filename);
834 if (setup_data_load(setup_data, type, filename)) {
835 if (opt_quiet)
836 printf("Loading %s ", filename);
837 printf("failed\n");
838 return -1;
839 }
841 if (!opt_quiet)
842 printf("ok\n");
844 return 0;
845 }
847 static int main_linux(int argc, char *argv[])
848 {
849 const char *kernel_name;
850 const char *initrd_name;
851 struct initramfs *initramfs;
852 struct setup_data *setup_data;
853 char *cmdline;
854 char *boot_image;
855 void *kernel_data;
856 size_t kernel_len;
857 bool opt_dhcpinfo = false;
858 bool opt_quiet = false;
859 void *dhcpdata;
860 size_t dhcplen;
861 char **argp, **argl, *arg, *p;
863 openconsole(&dev_null_r, &dev_stdcon_w);
865 (void)argc;
866 argp = argv + 1;
868 while ((arg = *argp) && arg[0] == '-') {
869 if (!strcmp("-dhcpinfo", arg)) {
870 opt_dhcpinfo = true;
871 } else {
872 fprintf(stderr, "%s: unknown option: %s\n", progname, arg);
873 return 1;
874 }
875 argp++;
876 }
878 if (!arg) {
879 fprintf(stderr, "%s: missing kernel name\n", progname);
880 return 1;
881 }
883 kernel_name = bestextfilename(arg);
885 errno = 0;
886 boot_image = malloc(strlen(kernel_name) + 12);
887 if (!boot_image) {
888 fprintf(stderr, "Error allocating BOOT_IMAGE string: ");
889 goto bail;
890 }
891 strcpy(boot_image, "BOOT_IMAGE=");
892 strcpy(boot_image + 11, kernel_name);
893 /* argp now points to the kernel name, and the command line follows.
894 Overwrite the kernel name with the BOOT_IMAGE= argument, and thus
895 we have the final argument. */
896 *argp = boot_image;
898 if (find_boolean(argp, "quiet"))
899 opt_quiet = true;
901 if (!opt_quiet)
902 printf("Loading %s... ", kernel_name);
903 errno = 0;
904 if (loadfile(kernel_name, &kernel_data, &kernel_len)) {
905 if (opt_quiet)
906 printf("Loading %s ", kernel_name);
907 printf("failed: ");
908 goto bail;
909 }
910 if (!opt_quiet)
911 printf("ok\n");
913 errno = 0;
914 cmdline = make_cmdline(argp);
915 if (!cmdline) {
916 fprintf(stderr, "make_cmdline() failed: ");
917 goto bail;
918 }
920 /* Initialize the initramfs chain */
921 errno = 0;
922 initramfs = initramfs_init();
923 if (!initramfs) {
924 fprintf(stderr, "initramfs_init() failed: ");
925 goto bail;
926 }
928 if ((arg = find_argument(argp, "initrd="))) {
929 do {
930 p = strchr(arg, ',');
931 if (p)
932 *p = '\0';
934 initrd_name = bestextfilename(arg);
935 if (!opt_quiet)
936 printf("Loading %s... ", initrd_name);
937 errno = 0;
938 if (initramfs_load_archive(initramfs, initrd_name)) {
939 if (opt_quiet)
940 printf("Loading %s ", initrd_name);
941 printf("failed: ");
942 goto bail;
943 }
944 if (!opt_quiet)
945 printf("ok\n");
947 if (p)
948 *p++ = ',';
949 } while ((arg = p));
950 }
952 /* Append the DHCP info */
953 if (opt_dhcpinfo &&
954 !pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
955 errno = 0;
956 if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
957 "/dhcpinfo.dat", 0, 0755)) {
958 fprintf(stderr, "Unable to add DHCP info: ");
959 goto bail;
960 }
961 }
963 /* Handle dtb and eventually other setup data */
964 setup_data = setup_data_init();
965 if (!setup_data)
966 goto bail;
968 for (argl = argv; (arg = *argl); argl++) {
969 if (!memcmp(arg, "dtb=", 4)) {
970 if (setup_data_file(setup_data, SETUP_DTB, arg+4, opt_quiet))
971 goto bail;
972 } else if (!memcmp(arg, "blob.", 5)) {
973 uint32_t type;
974 char *ep;
976 type = strtoul(arg + 5, &ep, 10);
977 if (ep[0] != '=' || !ep[1])
978 continue;
980 if (!type)
981 continue;
983 if (setup_data_file(setup_data, type, ep+1, opt_quiet))
984 goto bail;
985 }
986 }
988 /* This should not return... */
989 errno = 0;
990 syslinux_boot_linux(kernel_data, kernel_len, initramfs,
991 setup_data, cmdline);
992 fprintf(stderr, "syslinux_boot_linux() failed: ");
994 bail:
995 switch(errno) {
996 case ENOENT:
997 fprintf(stderr, "File not found\n");
998 break;
999 case ENOMEM:
1000 fprintf(stderr, "Out of memory\n");
1001 break;
1002 default:
1003 fprintf(stderr, "Error %d\n", errno);
1004 break;
1006 fprintf(stderr, "%s: Boot aborted!\n", progname);
1007 return 1;
1010 static int main_setarg(int argc, char *argv[])
1012 if (argc < 3) {
1013 usage("Usage: setarg.c32 argnum [args]...");
1015 setlinuxarg(atoi(argv[1]), argc - 2, argv + 2);
1016 return 0;
1019 static int main_ifarg(int argc, char *argv[])
1021 int i;
1022 size_t size;
1024 if (argc < 3) {
1025 usage("Usage: ifarg.c32 [argnum labelifset]... labelifnoneset");
1027 for (i = 1; i < argc - 1; i += 2) {
1028 int n = atoi(argv[i]);
1029 if (n == -1) {
1030 for (n = 0; n < 255; n++) {
1031 if (syslinux_getadv(n, &size))
1032 goto found;
1034 continue;
1036 else if (! syslinux_getadv(n, &size)) continue;
1037 found:
1038 syslinux_run_command(argv[i+1]);
1040 if (i != argc) syslinux_run_command(argv[i]);
1041 else syslinux_run_default();
1042 return 0;
1045 /* ----------------------------------------------------------------------- *
1047 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
1049 * This program is free software; you can redistribute it and/or modify
1050 * it under the terms of the GNU General Public License as published by
1051 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
1052 * Boston MA 02111-1307, USA; either version 2 of the License, or
1053 * (at your option) any later version; incorporated herein by reference.
1055 * ----------------------------------------------------------------------- */
1057 static int main_listarg(int argc, char *argv[])
1059 uint8_t *p, *ep;
1060 size_t s = syslinux_adv_size();
1061 char buf[256];
1063 (void) argc;
1064 (void) argv;
1065 p = syslinux_adv_ptr();
1067 printf("args size: %zd bytes at %p\n", s, p);
1069 ep = p + s; /* Need at least opcode+len */
1070 while (p < ep - 1 && *p) {
1071 int t = *p++;
1072 int l = *p++;
1074 if (p + l > ep)
1075 break;
1077 memcpy(buf, p, l);
1078 buf[l] = '\0';
1080 printf("arg %3d: \"%s\"\n", t, buf);
1082 p += l;
1084 sleep(5);
1085 return 0;
1088 int main(int argc, char *argv[])
1090 unsigned i;
1091 static struct {
1092 char *name;
1093 int (*main)(int argc, char *argv[]);
1094 } bin[] = {
1095 { "say", main_say },
1096 { "md5sum", main_md5sum },
1097 { "ifmem", main_ifmem },
1098 { "reboot", main_reboot },
1099 { "poweroff", main_poweroff },
1100 { "kbdmap", main_kbdmap },
1101 { "linux", main_linux },
1102 { "setarg", main_setarg },
1103 { "ifarg", main_ifarg },
1104 { "listarg", main_listarg }
1105 };
1107 openconsole(&dev_null_r, &dev_stdcon_w);
1109 if (strstr(argv[0], "c32box")) { argc--; argv++; }
1110 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1111 if (strstr(argv[0], bin[i].name))
1112 return bin[i].main(argc, argv);
1113 printf("No %s in c32box modules\n", argv[0]);
1114 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1115 printf(" %s \n",bin[i].name);
1116 return 1;