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

syslinux: fix isofs checksum
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Jul 14 10:16:48 2016 +0200 (2016-07-14)
parents 3b9bfe76d680
children 0253b140a342
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>
26 #include <syslinux/config.h>
27 #include <syslinux/disk.h>
29 #define ALIGN1
31 static uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
32 static uint64_t total64; /* must be directly before hash[] */
33 static uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
35 /* Emit a string of hex representation of bytes */
36 static char* bin2hex(char *p)
37 {
38 static const char bb_hexdigits_upcase[] ALIGN1 = "0123456789abcdef";
39 int count = 16;
40 const char *cp = (const char *) hash;
41 while (count) {
42 unsigned char c = *cp++;
43 /* put lowercase hex digits */
44 *p++ = bb_hexdigits_upcase[c >> 4];
45 *p++ = bb_hexdigits_upcase[c & 0xf];
46 count--;
47 }
48 return p;
49 }
51 //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
52 static uint32_t rotl32(uint32_t x, unsigned n)
53 {
54 return (x << n) | (x >> (32 - n));
55 }
57 static void md5_process_block64(void);
59 /* Feed data through a temporary buffer.
60 * The internal buffer remembers previous data until it has 64
61 * bytes worth to pass on.
62 */
63 static void common64_hash(const void *buffer, size_t len)
64 {
65 unsigned bufpos = total64 & 63;
67 total64 += len;
69 while (1) {
70 unsigned remaining = 64 - bufpos;
71 if (remaining > len)
72 remaining = len;
73 /* Copy data into aligned buffer */
74 memcpy(wbuffer + bufpos, buffer, remaining);
75 len -= remaining;
76 buffer = (const char *)buffer + remaining;
77 bufpos += remaining;
78 /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
79 bufpos -= 64;
80 if (bufpos != 0)
81 break;
82 /* Buffer is filled up, process it */
83 md5_process_block64();
84 /*bufpos = 0; - already is */
85 }
86 }
88 /* Process the remaining bytes in the buffer */
89 static void common64_end(void)
90 {
91 unsigned bufpos = total64 & 63;
92 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
93 wbuffer[bufpos++] = 0x80;
95 /* This loop iterates either once or twice, no more, no less */
96 while (1) {
97 unsigned remaining = 64 - bufpos;
98 memset(wbuffer + bufpos, 0, remaining);
99 /* Do we have enough space for the length count? */
100 if (remaining >= 8) {
101 /* Store the 64-bit counter of bits in the buffer */
102 uint64_t t = total64 << 3;
103 /* wbuffer is suitably aligned for this */
104 *(uint64_t *) (&wbuffer[64 - 8]) = t;
105 }
106 md5_process_block64();
107 if (remaining >= 8)
108 break;
109 bufpos = 0;
110 }
111 }
113 /* These are the four functions used in the four steps of the MD5 algorithm
114 * and defined in the RFC 1321. The first function is a little bit optimized
115 * (as found in Colin Plumbs public domain implementation).
116 * #define FF(b, c, d) ((b & c) | (~b & d))
117 */
118 #undef FF
119 #undef FG
120 #undef FH
121 #undef FI
122 #define FF(b, c, d) (d ^ (b & (c ^ d)))
123 #define FG(b, c, d) FF(d, b, c)
124 #define FH(b, c, d) (b ^ c ^ d)
125 #define FI(b, c, d) (c ^ (b | ~d))
127 /* Hash a single block, 64 bytes long and 4-byte aligned */
128 static void md5_process_block64(void)
129 {
130 /* Before we start, one word to the strange constants.
131 They are defined in RFC 1321 as
132 T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
133 */
134 static const uint32_t C_array[] = {
135 /* round 1 */
136 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
137 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
138 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
139 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
140 /* round 2 */
141 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
142 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
143 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
144 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
145 /* round 3 */
146 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
147 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
148 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
149 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
150 /* round 4 */
151 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
152 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
153 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
154 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
155 };
156 static const char P_array[] ALIGN1 = {
157 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
158 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
159 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
160 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
161 };
162 uint32_t *words = (void*) wbuffer;
163 uint32_t A = hash[0];
164 uint32_t B = hash[1];
165 uint32_t C = hash[2];
166 uint32_t D = hash[3];
168 static const char S_array[] ALIGN1 = {
169 7, 12, 17, 22,
170 5, 9, 14, 20,
171 4, 11, 16, 23,
172 6, 10, 15, 21
173 };
174 const uint32_t *pc;
175 const char *pp;
176 const char *ps;
177 int i;
178 uint32_t temp;
181 pc = C_array;
182 pp = P_array;
183 ps = S_array - 4;
185 for (i = 0; i < 64; i++) {
186 if ((i & 0x0f) == 0)
187 ps += 4;
188 temp = A;
189 switch (i >> 4) {
190 case 0:
191 temp += FF(B, C, D);
192 break;
193 case 1:
194 temp += FG(B, C, D);
195 break;
196 case 2:
197 temp += FH(B, C, D);
198 break;
199 case 3:
200 temp += FI(B, C, D);
201 }
202 temp += words[(int) (*pp++)] + *pc++;
203 temp = rotl32(temp, ps[i & 3]);
204 temp += B;
205 A = D;
206 D = C;
207 C = B;
208 B = temp;
209 }
210 /* Add checksum to the starting values */
211 hash[0] += A;
212 hash[1] += B;
213 hash[2] += C;
214 hash[3] += D;
216 }
217 #undef FF
218 #undef FG
219 #undef FH
220 #undef FI
222 /* Initialize structure containing state of computation.
223 * (RFC 1321, 3.3: Step 3)
224 */
225 static void md5_begin(void)
226 {
227 hash[0] = 0x67452301;
228 hash[1] = 0xefcdab89;
229 hash[2] = 0x98badcfe;
230 hash[3] = 0x10325476;
231 total64 = 0;
232 }
234 /* Used also for sha1 and sha256 */
235 #define md5_hash common64_hash
237 /* Process the remaining bytes in the buffer and put result from CTX
238 * in first 16 bytes following RESBUF. The result is always in little
239 * endian byte order, so that a byte-wise output yields to the wanted
240 * ASCII representation of the message digest.
241 */
242 #define md5_end common64_end
244 /*
245 * Copyright (C) 2003 Glenn L. McGrath
246 * Copyright (C) 2003-2004 Erik Andersen
247 *
248 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
249 */
251 static char *unrockridge(const char *name)
252 {
253 static char buffer[256];
254 int i = 0, j = 8;
255 while (*name && i < 255) {
256 char c = *name++;
257 //if (c == '\\') c = '/';
258 if (c == '.') {
259 for (j = i; --j >= 0 && buffer[j] != '/';)
260 if (buffer[j] == '.') buffer[j] = '_';
261 if (i - j > 9) i = j + 9;
262 j = i + 4;
263 }
264 else if (c == '/') j = i + 9;
265 else if (i >= j) continue;
266 else if (c >= 'a' && c <= 'z') c += 'A' - 'a';
267 else if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) c = '_';
268 buffer[i++] = c;
269 }
270 buffer[i] = 0;
271 return buffer;
272 }
274 static uint8_t *hash_file(const char *filename)
275 {
276 int src_fd, count;
277 uint8_t in_buf[4096];
278 static uint8_t hash_value[16*2+1];
280 src_fd = open(filename, O_RDONLY);
281 if (src_fd < 0) {
282 src_fd = open(unrockridge(filename), O_RDONLY);
283 }
284 if (src_fd < 0) {
285 return NULL;
286 }
288 md5_begin();
289 while ((count = read(src_fd, in_buf, 4096)) > 0) {
290 md5_hash(in_buf, count);
291 }
293 close(src_fd);
295 if (count)
296 return NULL;
298 md5_end();
299 bin2hex((char *)hash_value);
301 return hash_value;
302 }
304 static int main_say(int argc, char **argv)
305 {
306 int i;
307 for (i = 1; i < argc; i++) {
308 printf("%s ",argv[i]);
309 }
310 sleep(5);
311 return 0;
312 }
314 static int main_md5sum(int argc, char **argv)
315 {
316 int files = 0, tested = 0, good = 0;
317 static char clear_eol[] = " ";
319 (void) argc;
320 /* -c implied */
321 argv++;
322 do {
323 FILE *fp;
324 char eol, *line, buffer[4096];
325 fp = fopen(*argv,"r");
326 if (fp == NULL)
327 fp = fopen(unrockridge(*argv),"r");
329 while ((line = fgets(buffer,sizeof(buffer),fp)) != NULL) {
330 uint8_t *hash_value;
331 char *filename_ptr, *status;
332 int len = strlen(line);
334 if (line[0] < '0')
335 continue;
336 if (line[len-1] == '\n')
337 line[len-1] = 0;
338 filename_ptr = strstr(line, " ");
339 /* handle format for binary checksums */
340 if (filename_ptr == NULL) {
341 filename_ptr = strstr(line, " *");
342 }
343 if (filename_ptr == NULL) {
344 continue;
345 }
346 *filename_ptr = '\0';
347 *++filename_ptr = '/';
348 if (filename_ptr[1] == '/')
349 filename_ptr++;
351 files++;
352 status = "NOT CHECKED";
353 eol = '\n';
354 hash_value = hash_file(filename_ptr);
355 if (hash_value) {
356 tested++;
357 status = "BROKEN";
358 if (!strcmp((char*)hash_value, line)) {
359 good++;
360 status = "OK";
361 eol = ' ';
362 }
363 }
364 printf("\r%s: %s%s%c", filename_ptr, status, clear_eol, eol);
365 }
366 fclose(fp);
367 } while (*++argv);
368 printf("\r%d files OK, %d broken, %d not checked.%s\n",
369 good, tested - good, files - tested, clear_eol);
370 sleep(5);
371 return 0;
372 }
374 /*
375 * ifmem.c
376 *
377 * Run one command if the memory is large enought, and another if it isn't.
378 *
379 * Usage:
380 *
381 * label boot_kernel
382 * kernel ifmem.c
383 * append size_in_KB boot_large [size_in_KB boot_medium] boot_small
384 *
385 * label boot_large
386 * kernel vmlinuz_large_memory
387 * append ...
388 *
389 * label boot_small
390 * kernel vmlinuz_small_memory
391 * append ...
392 */
394 #include <inttypes.h>
395 #include <alloca.h>
396 #include <syslinux/boot.h>
398 struct e820_data {
399 uint64_t base;
400 uint64_t len;
401 uint32_t type;
402 uint32_t extattr;
403 } __attribute__((packed));
405 // Get memory size in Kb
406 static unsigned long memory_size(void)
407 {
408 uint64_t bytes = 0;
409 static com32sys_t ireg, oreg;
410 static struct e820_data ed;
412 ireg.eax.w[0] = 0xe820;
413 ireg.edx.l = 0x534d4150;
414 ireg.ecx.l = sizeof(struct e820_data);
415 ireg.edi.w[0] = OFFS(__com32.cs_bounce);
416 ireg.es = SEG(__com32.cs_bounce);
418 ed.extattr = 1;
420 do {
421 memcpy(__com32.cs_bounce, &ed, sizeof ed);
423 __intcall(0x15, &ireg, &oreg);
424 if (oreg.eflags.l & EFLAGS_CF ||
425 oreg.eax.l != 0x534d4150 ||
426 oreg.ecx.l < 20)
427 break;
429 memcpy(&ed, __com32.cs_bounce, sizeof ed);
431 if (ed.type == 1)
432 bytes += ed.len;
434 ireg.ebx.l = oreg.ebx.l;
435 } while (ireg.ebx.l);
437 if (!bytes) {
438 memset(&ireg, 0, sizeof ireg);
439 ireg.eax.w[0] = 0x8800;
440 __intcall(0x15, &ireg, &oreg);
441 return ireg.eax.w[0];
442 }
443 return bytes >> 10;
444 }
446 static void usage(const char *msg)
447 {
448 fprintf(stderr,"\n%s\n.",msg);
449 sleep(5);
450 exit(1);
451 }
453 static int main_ifmem(int argc, char *argv[])
454 {
455 int i;
456 unsigned long ram_size;
458 if (argc < 4) {
459 usage("Usage: ifmem.c32 size_KB boot_large_memory boot_small_memory");
460 }
462 // find target according to ram size
463 ram_size = memory_size();
464 printf("Total memory found %luK.\n",ram_size);
465 ram_size += (1 << 10); // add 1M to round boundaries...
467 i = 1;
468 do {
469 char *s = argv[i];
470 char *p = s;
471 unsigned long scale = 1;
473 while (*p >= '0' && *p <= '9') p++;
474 switch (*p | 0x20) {
475 case 'g': scale <<= 10;
476 case 'm': scale <<= 10;
477 default : *p = 0; break;
478 }
479 i++; // seek to label
480 if (ram_size >= scale * strtoul(s, NULL, 0)) break;
481 i++; // next size or default label
482 } while (i + 1 < argc);
484 if (i != argc) syslinux_run_command(argv[i]);
485 else syslinux_run_default();
486 return -1;
487 }
489 #include <syslinux/reboot.h>
491 static int main_reboot(int argc, char *argv[])
492 {
493 int warm = 0;
494 int i;
496 for (i = 1; i < argc; i++) {
497 if (!strcmp(argv[i], "-w") || !strcmp(argv[i], "--warm"))
498 warm = 1;
499 }
501 syslinux_reboot(warm);
502 }
504 /* APM poweroff module.
505 * based on poweroff.asm, Copyright 2009 Sebastian Herbszt
506 */
508 static int main_poweroff(int argc, char *argv[])
509 {
510 static com32sys_t ireg, oreg;
511 static char notsupported[] ="APM 1.1+ not supported";
512 unsigned i;
513 static struct {
514 unsigned short ax;
515 unsigned short bx;
516 unsigned short cx;
517 char *msg;
518 } inst[] = {
519 { 0x5300, // APM Installation Check (00h)
520 0, // APM BIOS (0000h)
521 0, "APM not present" },
522 { 0x5301, // APM Real Mode Interface Connect (01h)
523 0, // APM BIOS (0000h)
524 0, "APM RM interface connect failed" },
525 { 0x530E, // APM Driver Version (0Eh)
526 0, // APM BIOS (0000h)
527 0x0101, // APM Driver Version version 1.1
528 notsupported },
529 { 0x5307, // Set Power State (07h)
530 1, // All devices power managed by the APM
531 3, // Power state off
532 "Power off failed" }
533 };
535 (void) argc;
536 (void) argv;
537 for (i = 0; i < sizeof(inst)/sizeof(inst[0]); i++) {
538 char *msg = inst[i].msg;
540 ireg.eax.w[0] = inst[i].ax;
541 ireg.ebx.w[0] = inst[i].bx;
542 ireg.ecx.w[0] = inst[i].cx;
543 __intcall(0x15, &ireg, &oreg);
544 if ((oreg.eflags.l & EFLAGS_CF) == 0) {
545 switch (inst[i].ax) {
546 case 0x5300 :
547 if (oreg.ebx.w[0] != 0x504D /* 'PM' */) break;
548 msg = "Power management disabled";
549 if (oreg.ecx.w[0] & 8) break; // bit 3 APM BIOS Power Management disabled
550 case 0x530E :
551 msg = notsupported;
552 if (oreg.eax.w[0] < 0x101) break;
553 default : continue;
554 }
555 }
556 printf("%s.\n", msg);
557 return 1;
558 }
559 return 0;
560 }
562 /*
563 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
564 */
566 #include <syslinux/keyboard.h>
567 #include <syslinux/loadfile.h>
568 #include <syslinux/adv.h>
570 static void setlinuxarg(int slot, int argc, char *argv[])
571 {
572 for (; argc--; argv++)
573 syslinux_setadv(slot++, strlen(*argv), *argv);
574 }
576 #include "../../core/unlzma.c"
577 static int main_kbdmap(int argc, char *argv[])
578 {
579 const struct syslinux_keyboard_map *const kmap = syslinux_keyboard_map();
580 size_t map_size, size, i;
581 char *kbdmap, *msg;
583 if (argc < 3)
584 usage("Usage: kbdmap archive.cpio mapfile [cmdline]..");
586 // Save extra cmdline arguments
587 setlinuxarg(1, argc - 3, argv + 3);
589 msg="Append to kernel parameters: ";
590 for (i = 3; i < (size_t) argc; i++, msg = " ")
591 printf("%s%s",msg,argv[i]);
592 printf("\n\n Hit RETURN to continue.\n");
594 msg = "Load error";
595 if (kmap->version != 1 ||
596 loadfile(argv[1], (void **) &kbdmap, &map_size))
597 goto kbdmap_error;
598 if (* (short *) kbdmap == 0x005D) {
599 void *p = malloc(map_size = * (long *) (kbdmap + 5));
600 void *heap = malloc(2*(1846 + (768 << (3 + 0))) + 16);
602 unlzma(kbdmap, p, heap);
603 free(heap);
604 free(kbdmap);
605 kbdmap = p;
606 }
607 if (strncmp(kbdmap, "07070", 5))
608 goto kbdmap_error;
610 // search for mapfile in cpio archive
611 for (i = 0; i < map_size;) {
612 int len, j;
613 char *name;
615 for (j = size = 0; j < 8; j++) {
616 char c = kbdmap[54 + i + j] - '0';
617 if (c > 9) c += '0' + 10 - 'A';
618 size <<= 4;
619 size += c;
620 }
621 i += 110;
622 name = kbdmap + i;
623 len = 1 + strlen(name);
624 i += len;
625 i += ((-i)&3);
626 if (!strcmp(name, argv[2])) {
627 kbdmap += i;
628 break;
629 }
630 i += size + ((-size)&3);
631 }
633 msg = "Filename error";
634 if (i >= map_size)
635 goto kbdmap_error;
637 msg = "Format error";
638 if (size != kmap->length)
639 goto kbdmap_error;
641 memcpy(kmap->map, kbdmap, size);
643 return 0;
645 kbdmap_error:
646 printf("%s.\n",msg);
647 return 1;
648 }
650 /* ----------------------------------------------------------------------- *
651 *
652 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
653 * Copyright 2009-2012 Intel Corporation; author: H. Peter Anvin
654 *
655 * Permission is hereby granted, free of charge, to any person
656 * obtaining a copy of this software and associated documentation
657 * files (the "Software"), to deal in the Software without
658 * restriction, including without limitation the rights to use,
659 * copy, modify, merge, publish, distribute, sublicense, and/or
660 * sell copies of the Software, and to permit persons to whom
661 * the Software is furnished to do so, subject to the following
662 * conditions:
663 *
664 * The above copyright notice and this permission notice shall
665 * be included in all copies or substantial portions of the Software.
666 *
667 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
668 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
669 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
670 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
671 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
672 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
673 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
674 * OTHER DEALINGS IN THE SOFTWARE.
675 *
676 * ----------------------------------------------------------------------- */
678 /*
679 * linux.c
680 *
681 * Sample module to load Linux kernels. This module can also create
682 * a file out of the DHCP return data if running under PXELINUX.
683 *
684 * If -dhcpinfo is specified, the DHCP info is written into the file
685 * /dhcpinfo.dat in the initramfs.
686 *
687 * Usage: linux.c32 [-dhcpinfo] kernel arguments...
688 */
690 #include <errno.h>
691 #include <stdbool.h>
692 #include <stdlib.h>
693 #include <stdio.h>
694 #include <string.h>
695 #include <console.h>
696 #include <cpuid.h>
697 #include <syslinux/loadfile.h>
698 #include <syslinux/linux.h>
699 #include <syslinux/pxe.h>
701 const char *progname = "c32box.c32";
703 /* Find the last instance of a particular command line argument
704 (which should include the final =; do not use for boolean arguments) */
705 static char *find_argument(char **argv, const char *argument)
706 {
707 int la = strlen(argument);
708 char **arg;
709 char *ptr = NULL;
711 for (arg = argv; *arg; arg++) {
712 if (!memcmp(*arg, argument, la))
713 ptr = *arg + la;
714 }
716 return ptr;
717 }
719 /* Search for a boolean argument; return its position, or 0 if not present */
720 static int find_boolean(char **argv, const char *argument)
721 {
722 char **arg;
724 for (arg = argv; *arg; arg++) {
725 if (!strcmp(*arg, argument))
726 return (arg - argv) + 1;
727 }
729 return 0;
730 }
732 static int got_config;
733 static char *custom_cmdline = "";
734 static int custom_initrdlen;
735 static char *custom_initrdbase;
736 static char *custom_buffer;
737 static struct disk_info diskinfo;
739 static int has_custom_config(void)
740 {
741 const union syslinux_derivative_info *sdi;
742 int retry=0;
744 if (got_config)
745 goto done;
746 sdi = syslinux_derivative_info();
747 if (sdi->c.filesystem != SYSLINUX_FS_ISOLINUX)
748 goto fail;
749 disk_get_params(sdi->iso.drive_number, &diskinfo);
750 custom_buffer = disk_read_sectors(&diskinfo, 32768 / diskinfo.bps, 1);
751 got_config = (*(unsigned long *) (custom_buffer + 80) * 2048) / diskinfo.bps;
752 do {
753 free(custom_buffer);
754 custom_buffer = disk_read_sectors(&diskinfo, got_config, 1);
755 if (!memcmp(custom_buffer,"#!boot ",7)) {
756 char *p = custom_buffer+7+32+1;
758 if (!memcmp(p,"append=",7)) {
759 custom_cmdline = p + 7;
760 p = strchr(p,'\n');
761 *p++ = 0;
762 }
763 if (!memcmp(p,"initrd:",7)) {
764 custom_initrdlen = strtoul(p + 7, &custom_initrdbase, 10);
765 custom_initrdbase++;
766 }
767 return 1;
768 }
769 got_config += 16UL;
770 retry = 1 - retry;
771 } while (retry);
772 fail:
773 got_config = -1;
774 done:
775 return got_config > 0;
776 }
778 static int loadcustominitrd(void **data)
779 {
780 int n, len;
781 char *p;
783 p = *data = malloc(custom_initrdlen);
784 if (!p) return 0;
785 len = custom_initrdlen;
786 while (1) {
787 n = 2048 + custom_buffer - custom_initrdbase;
788 if (n > len)
789 n = len;
790 memcpy(p, custom_initrdbase, n);
791 p += n;
792 len -= n;
793 if (len == 0)
794 break;
795 free(custom_buffer);
796 got_config += 2048 / diskinfo.bps;
797 custom_initrdbase = custom_buffer =
798 disk_read_sectors(&diskinfo, got_config, 2048 / diskinfo.bps);
799 }
800 return 1;
801 }
803 /* Stitch together the command line from a set of argv's */
804 static char *make_cmdline(char **argv)
805 {
806 char **arg;
807 size_t bytes, size;
808 char *cmdline, *p;
809 int i;
811 bytes = 1; /* Just in case we have a zero-entry cmdline */
812 for (arg = argv; *arg; arg++) {
813 bytes += strlen(*arg) + 1;
814 }
815 for (i = 0; i < 255; i++)
816 if (syslinux_getadv(i, &size))
817 bytes += ++size;
818 if (has_custom_config())
819 bytes += strlen(custom_cmdline) + 1;
821 p = cmdline = malloc(bytes);
822 if (!cmdline)
823 return NULL;
825 for (arg = argv; *arg; arg++) {
826 int len = strlen(*arg);
827 memcpy(p, *arg, len);
828 p[len] = ' ';
829 p += len + 1;
830 }
832 for (i = 0; i < 255; i++) {
833 const void *q = syslinux_getadv(i, &size);
834 if (q == NULL) continue;
835 memcpy(p, q, size);
836 p[size] = ' ';
837 p += size + 1;
838 }
840 if (p > cmdline)
841 p--; /* Remove the last space */
842 *p = '\0';
844 if (has_custom_config()) {
845 *p++ = ' ';
846 strcpy(p, custom_cmdline);
847 }
849 return cmdline;
850 }
852 static bool __constfunc cpu_has_cpuid(void)
853 {
854 return cpu_has_eflag(X86_EFLAGS_ID);
855 }
857 static bool __constfunc cpu_has_level(uint32_t level)
858 {
859 uint32_t group;
860 uint32_t limit;
862 if (!cpu_has_cpuid())
863 return false;
865 group = level & 0xffff0000;
866 limit = cpuid_eax(group);
868 if ((limit & 0xffff0000) != group)
869 return false;
871 if (level > limit)
872 return false;
874 return true;
875 }
877 /* This only supports feature groups 0 and 1, corresponding to the
878 Intel and AMD EDX bit vectors. We can add more later if need be. */
879 static bool __constfunc cpu_has_feature(int x)
880 {
881 uint32_t level = ((x & 1) << 31) | 1;
883 return cpu_has_level(level) && ((cpuid_edx(level) >> (x & 31) & 1));
884 }
886 static const char *extfilename(const char *filename, char *ext, int feature)
887 {
888 #define NEWFILENAMESZ 256
889 static char newfilename[NEWFILENAMESZ+1];
890 const char *found = filename;
891 char *new = newfilename;
892 int fd;
894 if (strlen(filename) + strlen(ext) <= NEWFILENAMESZ) {
895 strcpy(newfilename, filename);
896 if (cpu_has_feature(feature)) {
897 strcat(newfilename, ext);
898 fd = open(new, O_RDONLY);
899 if (fd < 0)
900 fd = open(new = unrockridge(new), O_RDONLY);
901 if (fd >= 0) {
902 found = new;
903 close(fd);
904 }
905 }
906 }
907 return found;
908 }
910 static const char *bestextfilename(const char *filename)
911 {
912 const char *found;
914 //found = extfilename(filename, "fpu", X86_FEATURE_FPU);
915 //found = extfilename(filename, "686", X86_FEATURE_CMOV);
916 //found = extfilename(filename, "pae", X86_FEATURE_PAE);
917 found = extfilename(filename, "64", X86_FEATURE_LM);
918 //found = extfilename(filename, "guest", X86_FEATURE_HYPERVISOR);
919 return found;
920 }
922 static int setup_data_file(struct setup_data *setup_data,
923 uint32_t type, const char *filename,
924 bool opt_quiet)
925 {
926 if (!opt_quiet)
927 printf("Loading %s... ", filename);
929 if (setup_data_load(setup_data, type, filename)) {
930 if (opt_quiet)
931 printf("Loading %s ", filename);
932 printf("failed\n");
933 return -1;
934 }
936 if (!opt_quiet)
937 printf("ok\n");
939 return 0;
940 }
942 static int main_linux(int argc, char *argv[])
943 {
944 const char *kernel_name;
945 const char *initrd_name;
946 struct initramfs *initramfs;
947 struct setup_data *setup_data;
948 char *cmdline;
949 char *boot_image;
950 void *kernel_data;
951 size_t kernel_len;
952 bool opt_dhcpinfo = false;
953 bool opt_quiet = false;
954 void *dhcpdata;
955 size_t dhcplen;
956 char **argp, **argl, *arg, *p;
958 openconsole(&dev_null_r, &dev_stdcon_w);
960 (void)argc;
961 argp = argv + 1;
963 while ((arg = *argp) && arg[0] == '-') {
964 if (!strcmp("-dhcpinfo", arg)) {
965 opt_dhcpinfo = true;
966 } else {
967 fprintf(stderr, "%s: unknown option: %s\n", progname, arg);
968 return 1;
969 }
970 argp++;
971 }
973 if (!arg) {
974 fprintf(stderr, "%s: missing kernel name\n", progname);
975 return 1;
976 }
978 kernel_name = arg;
980 errno = 0;
981 boot_image = malloc(strlen(kernel_name) + 12);
982 if (!boot_image) {
983 fprintf(stderr, "Error allocating BOOT_IMAGE string: ");
984 goto bail;
985 }
986 strcpy(boot_image, "BOOT_IMAGE=");
987 strcpy(boot_image + 11, kernel_name);
988 /* argp now points to the kernel name, and the command line follows.
989 Overwrite the kernel name with the BOOT_IMAGE= argument, and thus
990 we have the final argument. */
991 *argp = boot_image;
993 if (find_boolean(argp, "quiet"))
994 opt_quiet = true;
996 if (!opt_quiet)
997 printf("Loading %s... ", kernel_name);
998 errno = 0;
999 if (loadfile(bestextfilename(kernel_name), &kernel_data, &kernel_len)) {
1000 if (opt_quiet)
1001 printf("Loading %s ", kernel_name);
1002 printf("failed: ");
1003 goto bail;
1005 if (!opt_quiet)
1006 printf("ok\n");
1008 errno = 0;
1009 cmdline = make_cmdline(argp);
1010 if (!cmdline) {
1011 fprintf(stderr, "make_cmdline() failed: ");
1012 goto bail;
1015 /* Initialize the initramfs chain */
1016 errno = 0;
1017 initramfs = initramfs_init();
1018 if (!initramfs) {
1019 fprintf(stderr, "initramfs_init() failed: ");
1020 goto bail;
1023 if ((arg = find_argument(argp, "initrd="))) {
1024 do {
1025 p = strchr(arg, ',');
1026 if (p)
1027 *p = '\0';
1029 initrd_name = arg;
1030 if (!opt_quiet)
1031 printf("Loading %s... ", initrd_name);
1032 errno = 0;
1033 if (initramfs_load_archive(initramfs, bestextfilename(initrd_name))) {
1034 if (opt_quiet)
1035 printf("Loading %s ", initrd_name);
1036 printf("failed: ");
1037 goto bail;
1039 if (!opt_quiet)
1040 printf("ok\n");
1042 if (p)
1043 *p++ = ',';
1044 } while ((arg = p));
1047 /* Append the DHCP info */
1048 if (opt_dhcpinfo &&
1049 !pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
1050 errno = 0;
1051 if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
1052 "/dhcpinfo.dat", 0, 0755)) {
1053 fprintf(stderr, "Unable to add DHCP info: ");
1054 goto bail;
1058 if (has_custom_config() && custom_initrdlen) {
1059 void *data;
1061 if (!opt_quiet)
1062 printf("Loading custom initrd... ");
1063 if (loadcustominitrd(&data))
1064 initramfs_add_data(initramfs, data, custom_initrdlen, custom_initrdlen, 4);
1067 /* Handle dtb and eventually other setup data */
1068 setup_data = setup_data_init();
1069 if (!setup_data)
1070 goto bail;
1072 for (argl = argv; (arg = *argl); argl++) {
1073 if (!memcmp(arg, "dtb=", 4)) {
1074 if (setup_data_file(setup_data, SETUP_DTB, arg+4, opt_quiet))
1075 goto bail;
1076 } else if (!memcmp(arg, "blob.", 5)) {
1077 uint32_t type;
1078 char *ep;
1080 type = strtoul(arg + 5, &ep, 10);
1081 if (ep[0] != '=' || !ep[1])
1082 continue;
1084 if (!type)
1085 continue;
1087 if (setup_data_file(setup_data, type, ep+1, opt_quiet))
1088 goto bail;
1092 /* This should not return... */
1093 errno = 0;
1094 syslinux_boot_linux(kernel_data, kernel_len, initramfs,
1095 setup_data, cmdline);
1096 fprintf(stderr, "syslinux_boot_linux() failed: ");
1098 bail:
1099 switch(errno) {
1100 case ENOENT:
1101 fprintf(stderr, "File not found\n");
1102 break;
1103 case ENOMEM:
1104 fprintf(stderr, "Out of memory\n");
1105 break;
1106 default:
1107 fprintf(stderr, "Error %d\n", errno);
1108 break;
1110 fprintf(stderr, "%luM RAM found on this %s bits machine.\n",
1111 memory_size() >> 10,
1112 cpu_has_feature(X86_FEATURE_LM) ? "64": "32");
1113 fprintf(stderr, "%s: Boot aborted!\n", progname);
1114 return 1;
1117 static int main_setarg(int argc, char *argv[])
1119 if (argc < 3) {
1120 usage("Usage: setarg.c32 argnum [args]...");
1122 setlinuxarg(atoi(argv[1]), argc - 2, argv + 2);
1123 return 0;
1126 static int main_ifarg(int argc, char *argv[])
1128 int i;
1129 size_t size;
1131 if (argc < 3) {
1132 usage("Usage: ifarg.c32 [argnum labelifset]... labelifnoneset");
1134 for (i = 1; i < argc - 1; i += 2) {
1135 int n = atoi(argv[i]);
1136 if (n == -1) {
1137 for (n = 0; n < 255; n++) {
1138 if (syslinux_getadv(n, &size))
1139 goto found;
1141 continue;
1143 else if (! syslinux_getadv(n, &size)) continue;
1144 found:
1145 syslinux_run_command(argv[i+1]);
1147 if (i != argc) syslinux_run_command(argv[i]);
1148 else syslinux_run_default();
1149 return 0;
1152 /* ----------------------------------------------------------------------- *
1154 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
1156 * This program is free software; you can redistribute it and/or modify
1157 * it under the terms of the GNU General Public License as published by
1158 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
1159 * Boston MA 02111-1307, USA; either version 2 of the License, or
1160 * (at your option) any later version; incorporated herein by reference.
1162 * ----------------------------------------------------------------------- */
1164 static int main_listarg(int argc, char *argv[])
1166 uint8_t *p, *ep;
1167 size_t s = syslinux_adv_size();
1168 char buf[256];
1170 (void) argc;
1171 (void) argv;
1172 p = syslinux_adv_ptr();
1174 printf("args size: %zd bytes at %p\n", s, p);
1176 ep = p + s; /* Need at least opcode+len */
1177 while (p < ep - 1 && *p) {
1178 int t = *p++;
1179 int l = *p++;
1181 if (p + l > ep)
1182 break;
1184 memcpy(buf, p, l);
1185 buf[l] = '\0';
1187 printf("arg %3d: \"%s\"\n", t, buf);
1189 p += l;
1191 sleep(5);
1192 return 0;
1195 int main(int argc, char *argv[])
1197 unsigned i;
1198 static struct {
1199 char *name;
1200 int (*main)(int argc, char *argv[]);
1201 } bin[] = {
1202 { "say", main_say },
1203 { "md5sum", main_md5sum },
1204 { "ifmem", main_ifmem },
1205 { "reboot", main_reboot },
1206 { "poweroff", main_poweroff },
1207 { "kbdmap", main_kbdmap },
1208 { "linux", main_linux },
1209 { "setarg", main_setarg },
1210 { "ifarg", main_ifarg },
1211 { "listarg", main_listarg }
1212 };
1214 openconsole(&dev_null_r, &dev_stdcon_w);
1216 if (strstr(argv[0], "c32box")) { argc--; argv++; }
1217 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1218 if (strstr(argv[0], bin[i].name))
1219 return bin[i].main(argc, argv);
1220 printf("No %s in c32box modules\n", argv[0]);
1221 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1222 printf(" %s \n",bin[i].name);
1223 return 1;