wok view syslinux/stuff/extra/md5sum.c @ rev 21485
libsdl2-ttf-dev: add pkgconfig
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed May 01 13:05:10 2019 +0200 (2019-05-01) |
parents | 0253b140a342 |
children | f063a9bbc7f4 |
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 #ifdef EXTRA
252 #define WITH_UNROCKRIDGE
253 #endif
254 #ifdef WITH_UNROCKRIDGE
255 static char *unrockridge(const char *name)
256 {
257 static char buffer[256];
258 int i = 0, j = 8;
259 while (*name && i < 255) {
260 char c = *name++;
261 //if (c == '\\') c = '/';
262 if (c == '.') {
263 for (j = i; --j >= 0 && buffer[j] != '/';)
264 if (buffer[j] == '.') buffer[j] = '_';
265 if (i - j > 9) i = j + 9;
266 j = i + 4;
267 }
268 else if (c == '/') j = i + 9;
269 else if (i >= j) continue;
270 else if (c >= 'a' && c <= 'z') c += 'A' - 'a';
271 else if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) c = '_';
272 buffer[i++] = c;
273 }
274 buffer[i] = 0;
275 return buffer;
276 }
277 #endif
279 static uint8_t *hash_file(const char *filename)
280 {
281 int src_fd, count;
282 uint8_t in_buf[4096];
283 static uint8_t hash_value[16*2+1];
285 src_fd = open(filename, O_RDONLY);
286 #ifdef WITH_UNROCKRIDGE
287 if (src_fd < 0) {
288 src_fd = open(unrockridge(filename), O_RDONLY);
289 }
290 #endif
292 if (src_fd < 0) {
293 return NULL;
294 }
296 md5_begin();
297 while ((count = read(src_fd, in_buf, 4096)) > 0) {
298 md5_hash(in_buf, count);
299 }
301 close(src_fd);
303 if (count)
304 return NULL;
306 md5_end();
307 bin2hex((char *)hash_value);
309 return hash_value;
310 }
312 #ifdef EXTRA
313 static int main_say(int argc, char **argv)
314 {
315 int i;
316 for (i = 1; i < argc; i++) {
317 printf("%s ",argv[i]);
318 }
319 sleep(5);
320 return 0;
321 }
322 #endif
324 static int main_md5sum(int argc, char **argv)
325 {
326 int files = 0, tested = 0, good = 0;
327 static char clear_eol[] = " ";
329 (void) argc;
330 /* -c implied */
331 argv++;
332 do {
333 FILE *fp;
334 char eol, *line, buffer[4096];
335 fp = fopen(*argv,"r");
336 #ifdef WITH_UNROCKRIDGE
337 if (fp == NULL)
338 fp = fopen(unrockridge(*argv),"r");
339 #endif
341 while ((line = fgets(buffer,sizeof(buffer),fp)) != NULL) {
342 uint8_t *hash_value;
343 char *filename_ptr, *status;
344 int len = strlen(line);
346 if (line[0] < '0')
347 continue;
348 if (line[len-1] == '\n')
349 line[len-1] = 0;
350 filename_ptr = strstr(line, " ");
351 /* handle format for binary checksums */
352 if (filename_ptr == NULL) {
353 filename_ptr = strstr(line, " *");
354 }
355 if (filename_ptr == NULL) {
356 continue;
357 }
358 *filename_ptr = '\0';
359 *++filename_ptr = '/';
360 if (filename_ptr[1] == '/')
361 filename_ptr++;
363 files++;
364 status = "NOT CHECKED";
365 eol = '\n';
366 hash_value = hash_file(filename_ptr);
367 if (hash_value) {
368 tested++;
369 status = "BROKEN";
370 if (!strcmp((char*)hash_value, line)) {
371 good++;
372 status = "OK";
373 eol = ' ';
374 }
375 }
376 printf("\r%s: %s%s%c", filename_ptr, status, clear_eol, eol);
377 }
378 fclose(fp);
379 } while (*++argv);
380 printf("\r%d files OK, %d broken, %d not checked.%s\n",
381 good, tested - good, files - tested, clear_eol);
382 sleep(5);
383 return 0;
384 }
386 /*
387 * ifmem.c
388 *
389 * Run one command if the memory is large enought, and another if it isn't.
390 *
391 * Usage:
392 *
393 * label boot_kernel
394 * kernel ifmem.c
395 * append size_in_KB boot_large [size_in_KB boot_medium] boot_small
396 *
397 * label boot_large
398 * kernel vmlinuz_large_memory
399 * append ...
400 *
401 * label boot_small
402 * kernel vmlinuz_small_memory
403 * append ...
404 */
406 #include <inttypes.h>
407 #include <alloca.h>
408 #include <syslinux/boot.h>
410 struct e820_data {
411 uint64_t base;
412 uint64_t len;
413 uint32_t type;
414 uint32_t extattr;
415 } __attribute__((packed));
417 // Get memory size in Kb
418 static unsigned long memory_size(void)
419 {
420 uint64_t bytes = 0;
421 static com32sys_t ireg, oreg;
422 static struct e820_data ed;
424 ireg.eax.w[0] = 0xe820;
425 ireg.edx.l = 0x534d4150;
426 ireg.ecx.l = sizeof(struct e820_data);
427 ireg.edi.w[0] = OFFS(__com32.cs_bounce);
428 ireg.es = SEG(__com32.cs_bounce);
430 ed.extattr = 1;
432 do {
433 memcpy(__com32.cs_bounce, &ed, sizeof ed);
435 __intcall(0x15, &ireg, &oreg);
436 if (oreg.eflags.l & EFLAGS_CF ||
437 oreg.eax.l != 0x534d4150 ||
438 oreg.ecx.l < 20)
439 break;
441 memcpy(&ed, __com32.cs_bounce, sizeof ed);
443 if (ed.type == 1)
444 bytes += ed.len;
446 ireg.ebx.l = oreg.ebx.l;
447 } while (ireg.ebx.l);
449 if (!bytes) {
450 memset(&ireg, 0, sizeof ireg);
451 ireg.eax.w[0] = 0x8800;
452 __intcall(0x15, &ireg, &oreg);
453 return ireg.eax.w[0];
454 }
455 return bytes >> 10;
456 }
458 static void usage(const char *msg)
459 {
460 fprintf(stderr,"\n%s\n.",msg);
461 sleep(5);
462 exit(1);
463 }
465 static int main_ifmem(int argc, char *argv[])
466 {
467 int i;
468 unsigned long ram_size;
470 if (argc < 4) {
471 usage("Usage: ifmem.c32 size_KB boot_large_memory boot_small_memory");
472 }
474 // find target according to ram size
475 ram_size = memory_size();
476 printf("Total memory found %luK.\n",ram_size);
477 ram_size += (1 << 10); // add 1M to round boundaries...
479 i = 1;
480 do {
481 char *s = argv[i];
482 char *p = s;
483 unsigned long scale = 1;
485 while (*p >= '0' && *p <= '9') p++;
486 switch (*p | 0x20) {
487 case 'g': scale <<= 10;
488 case 'm': scale <<= 10;
489 default : *p = 0; break;
490 }
491 i++; // seek to label
492 if (ram_size >= scale * strtoul(s, NULL, 0)) break;
493 i++; // next size or default label
494 } while (i + 1 < argc);
496 if (i != argc) syslinux_run_command(argv[i]);
497 else syslinux_run_default();
498 return -1;
499 }
501 #include <syslinux/reboot.h>
503 static int main_reboot(int argc, char *argv[])
504 {
505 int warm = 0;
506 int i;
508 for (i = 1; i < argc; i++) {
509 if (strstr(argv[i], "-w"))
510 warm = 1;
511 }
513 syslinux_reboot(warm);
514 }
516 /* APM poweroff module.
517 * based on poweroff.asm, Copyright 2009 Sebastian Herbszt
518 */
520 static int main_poweroff(int argc, char *argv[])
521 {
522 static com32sys_t ireg, oreg;
523 static char notsupported[] ="APM 1.1+ not supported";
524 unsigned i;
525 static struct {
526 unsigned short ax;
527 unsigned short bx;
528 unsigned short cx;
529 char *msg;
530 } inst[] = {
531 { 0x5300, // APM Installation Check (00h)
532 0, // APM BIOS (0000h)
533 0, "APM not present" },
534 { 0x5301, // APM Real Mode Interface Connect (01h)
535 0, // APM BIOS (0000h)
536 0, "APM RM interface connect failed" },
537 { 0x530E, // APM Driver Version (0Eh)
538 0, // APM BIOS (0000h)
539 0x0101, // APM Driver Version version 1.1
540 notsupported },
541 { 0x5307, // Set Power State (07h)
542 1, // All devices power managed by the APM
543 3, // Power state off
544 "Power off failed" }
545 };
547 (void) argc;
548 (void) argv;
549 for (i = 0; i < sizeof(inst)/sizeof(inst[0]); i++) {
550 char *msg = inst[i].msg;
552 ireg.eax.w[0] = inst[i].ax;
553 ireg.ebx.w[0] = inst[i].bx;
554 ireg.ecx.w[0] = inst[i].cx;
555 __intcall(0x15, &ireg, &oreg);
556 if ((oreg.eflags.l & EFLAGS_CF) == 0) {
557 switch (inst[i].ax) {
558 case 0x5300 :
559 if (oreg.ebx.w[0] != 0x504D /* 'PM' */) break;
560 msg = "Power management disabled";
561 if (oreg.ecx.w[0] & 8) break; // bit 3 APM BIOS Power Management disabled
562 case 0x530E :
563 msg = notsupported;
564 if (oreg.eax.w[0] < 0x101) break;
565 default : continue;
566 }
567 }
568 printf("%s.\n", msg);
569 return 1;
570 }
571 return 0;
572 }
574 /*
575 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
576 */
578 #include <syslinux/keyboard.h>
579 #include <syslinux/loadfile.h>
580 #include <syslinux/adv.h>
582 static void setlinuxarg(int slot, int argc, char *argv[])
583 {
584 for (; argc--; argv++)
585 syslinux_setadv(slot++, strlen(*argv), *argv);
586 }
588 #include "../../core/unlzma.c"
589 static int main_kbdmap(int argc, char *argv[])
590 {
591 const struct syslinux_keyboard_map *const kmap = syslinux_keyboard_map();
592 size_t map_size, size, i;
593 char *kbdmap, *msg, *kbdfile, *kbdname;
594 int skip = 3;
596 if (argc < 3)
597 usage("Usage: kbdmap [archive.cpio] [mapfile] [cmdline]..");
599 if (loadfile(kbdfile = argv[1], (void **) &kbdmap, &map_size)) {
600 kbdfile = "kbd";
601 skip--;
602 }
603 else {
604 free(kbdmap);
605 }
607 if (strchr(kbdname = argv[skip-1],'=')) {
608 for (i = --skip; argv[i]; i++) {
609 if (!strncmp(argv[i], "kmap=", 5)) {
610 kbdname = argv[i] + 5;
611 break;
612 }
613 }
614 }
616 // Save extra cmdline arguments
617 setlinuxarg(1, argc - skip, argv + skip);
619 msg="Append to kernel parameters: ";
620 for (i = skip; i < (size_t) argc; i++, msg = " ")
621 printf("%s%s",msg,argv[i]);
622 printf("\n\n Hit RETURN to continue.\n");
624 msg = "Load error";
625 if (kmap->version != 1 ||
626 loadfile(kbdfile, (void **) &kbdmap, &map_size))
627 goto kbdmap_error;
628 if (* (short *) kbdmap == 0x005D) {
629 void *p = malloc(map_size = * (long *) (kbdmap + 5));
630 void *heap = malloc(2*(1846 + (768 << (3 + 0))) + 16);
632 unlzma(kbdmap, p, heap);
633 free(heap);
634 free(kbdmap);
635 kbdmap = p;
636 }
637 if (strncmp(kbdmap, "07070", 5))
638 goto kbdmap_error;
640 // search for mapfile in cpio archive
641 for (i = 0; i < map_size;) {
642 int len, j;
643 char *name;
645 for (j = size = 0; j < 8; j++) {
646 char c = kbdmap[54 + i + j] - '0';
647 if (c > 9) c += '0' + 10 - 'A';
648 size <<= 4;
649 size += c;
650 }
651 i += 110;
652 name = kbdmap + i;
653 len = 1 + strlen(name);
654 i += len;
655 i += ((-i)&3);
656 if (!strcmp(name, kbdname)) {
657 kbdmap += i;
658 break;
659 }
660 i += size + ((-size)&3);
661 }
663 msg = "Filename error";
664 if (i >= map_size)
665 goto kbdmap_error;
667 msg = "Format error";
668 if (size != kmap->length)
669 goto kbdmap_error;
671 memcpy(kmap->map, kbdmap, size);
673 return 0;
675 kbdmap_error:
676 printf("%s.\n",msg);
677 return 1;
678 }
680 /* ----------------------------------------------------------------------- *
681 *
682 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
683 * Copyright 2009-2012 Intel Corporation; author: H. Peter Anvin
684 *
685 * Permission is hereby granted, free of charge, to any person
686 * obtaining a copy of this software and associated documentation
687 * files (the "Software"), to deal in the Software without
688 * restriction, including without limitation the rights to use,
689 * copy, modify, merge, publish, distribute, sublicense, and/or
690 * sell copies of the Software, and to permit persons to whom
691 * the Software is furnished to do so, subject to the following
692 * conditions:
693 *
694 * The above copyright notice and this permission notice shall
695 * be included in all copies or substantial portions of the Software.
696 *
697 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
698 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
699 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
700 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
701 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
702 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
703 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
704 * OTHER DEALINGS IN THE SOFTWARE.
705 *
706 * ----------------------------------------------------------------------- */
708 /*
709 * linux.c
710 *
711 * Sample module to load Linux kernels. This module can also create
712 * a file out of the DHCP return data if running under PXELINUX.
713 *
714 * If -dhcpinfo is specified, the DHCP info is written into the file
715 * /dhcpinfo.dat in the initramfs.
716 *
717 * Usage: linux.c32 [-dhcpinfo] kernel arguments...
718 */
720 #include <errno.h>
721 #include <stdbool.h>
722 #include <stdlib.h>
723 #include <stdio.h>
724 #include <string.h>
725 #include <console.h>
726 #include <cpuid.h>
727 #include <syslinux/loadfile.h>
728 #include <syslinux/linux.h>
729 #include <syslinux/pxe.h>
731 const char *progname = "c32box.c32";
733 /* Find the last instance of a particular command line argument
734 (which should include the final =; do not use for boolean arguments) */
735 static char *find_argument(char **argv, const char *argument)
736 {
737 int la = strlen(argument);
738 char **arg;
739 char *ptr = NULL;
741 for (arg = argv; *arg; arg++) {
742 if (!memcmp(*arg, argument, la))
743 ptr = *arg + la;
744 }
746 return ptr;
747 }
749 /* Search for a boolean argument; return its position, or 0 if not present */
750 #if 1
751 #define find_boolean(a,b) (find_argument(a,b) != NULL)
752 #else
753 static int find_boolean(char **argv, const char *argument)
754 {
755 char **arg;
757 for (arg = argv; *arg; arg++) {
758 if (!strcmp(*arg, argument))
759 return (arg - argv) + 1;
760 }
762 return 0;
763 }
764 #endif
766 static int got_config;
767 static char *custom_cmdline = "";
768 static int custom_initrdlen;
769 static int custom_initrdbase;
770 static char *custom_buffer;
771 static struct disk_info diskinfo;
773 static int has_custom_config(void)
774 {
775 const union syslinux_derivative_info *sdi;
777 if (got_config)
778 goto done;
779 got_config = -1;
780 sdi = syslinux_derivative_info();
781 if (sdi->c.filesystem != SYSLINUX_FS_ISOLINUX)
782 goto fail;
783 disk_get_params(sdi->iso.drive_number, &diskinfo);
784 custom_buffer = disk_read_sectors(&diskinfo, 32768 / diskinfo.bps, 1);
785 got_config = (*(unsigned long *) (custom_buffer + 80))
786 * 2048 / diskinfo.bps;
787 free(custom_buffer);
788 custom_buffer = disk_read_sectors(&diskinfo, got_config, 1);
789 if (!memcmp(custom_buffer,"#!boot ",7)) {
790 char *p = custom_buffer+7+32+1;
792 if (!memcmp(p,"append=",7)) {
793 custom_cmdline = p + 7;
794 p = strchr(p,'\n');
795 *p++ = 0;
796 }
797 if (!memcmp(p,"initrd:",7)) {
798 custom_initrdlen = strtoul(p + 7, &custom_initrdbase, 10);
799 custom_initrdbase += (got_config << 11) + 1 - (int) custom_buffer;
800 }
801 }
802 fail:
803 done:
804 return got_config > 0;
805 }
807 static int loadcustominitrd(void **data)
808 {
809 int n, len;
810 char *p;
812 p = *data = malloc(custom_initrdlen);
813 if (!p) return 0;
814 for (len = custom_initrdlen; len != 0; len -= n, p += n) {
815 free(custom_buffer);
816 custom_buffer = disk_read_sectors(&diskinfo, custom_initrdbase >> 11,
817 2048 / diskinfo.bps);
818 n = 2048 - (custom_initrdbase & 2047);
819 if (n > len)
820 n = len;
821 memcpy(p, custom_buffer + (custom_initrdbase & 2047), n);
822 custom_initrdbase += n;
823 }
824 return 1;
825 }
827 /* Stitch together the command line from a set of argv's */
828 static char *make_cmdline(char **argv)
829 {
830 char **arg;
831 size_t bytes, size;
832 char *cmdline, *p;
833 int i;
835 bytes = 1; /* Just in case we have a zero-entry cmdline */
836 for (arg = argv; *arg; arg++) {
837 bytes += strlen(*arg) + 1;
838 }
839 for (i = 0; i < 255; i++)
840 if (syslinux_getadv(i, &size))
841 bytes += ++size;
842 if (has_custom_config())
843 bytes += strlen(custom_cmdline) + 1;
845 p = cmdline = malloc(bytes);
846 if (!cmdline)
847 return NULL;
849 for (arg = argv; *arg; arg++) {
850 size = strlen(*arg);
851 memcpy(p, *arg, size);
852 p[size] = ' ';
853 p += size + 1;
854 }
856 for (i = 0; i < 255; i++) {
857 const void *q = syslinux_getadv(i, &size);
858 if (q == NULL) continue;
859 memcpy(p, q, size);
860 p[size] = ' ';
861 p += size + 1;
862 }
864 if (p > cmdline)
865 p--; /* Remove the last space */
866 *p = '\0';
868 if (has_custom_config()) {
869 *p++ = ' ';
870 strcpy(p, custom_cmdline);
871 }
873 return cmdline;
874 }
876 #define cpu_has_cpuid() cpu_has_eflag(X86_EFLAGS_ID)
878 static bool __constfunc cpu_has_level(uint32_t level)
879 {
880 uint32_t group;
881 uint32_t limit;
883 if (!cpu_has_cpuid())
884 return false;
886 group = level & 0xffff0000;
887 limit = cpuid_eax(group);
889 if ((limit & 0xffff0000) != group)
890 return false;
892 if (level > limit)
893 return false;
895 return true;
896 }
898 /* This only supports feature groups 0 and 1, corresponding to the
899 Intel and AMD EDX bit vectors. We can add more later if need be. */
900 static bool __constfunc cpu_has_feature(int x)
901 {
902 uint32_t level = ((x & 1) << 31) | 1;
904 return cpu_has_level(level) && ((cpuid_edx(level) >> (x & 31) & 1));
905 }
907 static const char *extfilename(const char *filename, char *ext, int feature)
908 {
909 #define NEWFILENAMESZ 256
910 static char newfilename[NEWFILENAMESZ+1];
911 const char *found = filename;
912 char *new = newfilename;
913 int fd;
915 if (strlen(filename) + strlen(ext) <= NEWFILENAMESZ) {
916 strcpy(newfilename, filename);
917 if (cpu_has_feature(feature)) {
918 strcat(newfilename, ext);
919 fd = open(new, O_RDONLY);
920 #ifdef WITH_UNROCKRIDGE
921 if (fd < 0)
922 fd = open(new = unrockridge(new), O_RDONLY);
923 #endif
924 if (fd >= 0) {
925 found = new;
926 close(fd);
927 }
928 }
929 }
930 return found;
931 }
933 static const char *bestextfilename(const char *filename)
934 {
935 const char *found;
937 //found = extfilename(filename, "fpu", X86_FEATURE_FPU);
938 //found = extfilename(filename, "686", X86_FEATURE_CMOV);
939 //found = extfilename(filename, "pae", X86_FEATURE_PAE);
940 found = extfilename(filename, "64", X86_FEATURE_LM);
941 //found = extfilename(filename, "guest", X86_FEATURE_HYPERVISOR);
942 return found;
943 }
945 static int setup_data_file(struct setup_data *setup_data,
946 uint32_t type, const char *filename,
947 bool opt_quiet)
948 {
949 if (!opt_quiet)
950 printf("Loading %s... ", filename);
952 if (setup_data_load(setup_data, type, filename)) {
953 if (opt_quiet)
954 printf("Loading %s ", filename);
955 printf("failed\n");
956 return -1;
957 }
959 if (!opt_quiet)
960 printf("ok\n");
962 return 0;
963 }
965 static int main_linux(int argc, char *argv[])
966 {
967 const char *kernel_name;
968 const char *initrd_name;
969 struct initramfs *initramfs;
970 struct setup_data *setup_data;
971 char *errmsg;
972 char *cmdline;
973 char *boot_image;
974 void *kernel_data;
975 size_t kernel_len;
976 bool opt_dhcpinfo = false;
977 bool opt_quiet = false;
978 void *dhcpdata;
979 size_t dhcplen;
980 char **argp, **argl, *arg, *p;
982 openconsole(&dev_null_r, &dev_stdcon_w);
984 (void)argc;
986 for (argp = argv + 1; (arg = *argp) && arg[0] == '-'; argp++) {
987 if (!strcmp("-dhcpinfo", arg)) {
988 opt_dhcpinfo = true;
989 } else {
990 errmsg = "%s: unknown option: %s\n";
991 goto unknown_option;
992 }
993 }
995 if (!arg) {
996 errmsg = "%s: missing kernel name\n";
997 unknown_option:
998 fprintf(stderr, errmsg, progname, arg);
999 return 1;
1000 }
1002 kernel_name = arg;
1004 errno = 0;
1005 boot_image = malloc(strlen(kernel_name) + 12);
1006 if (!boot_image) {
1007 errmsg = "Error allocating BOOT_IMAGE string: ";
1008 goto bailmsg;
1009 }
1010 strcpy(boot_image, "BOOT_IMAGE=");
1011 strcpy(boot_image + 11, kernel_name);
1012 /* argp now points to the kernel name, and the command line follows.
1013 Overwrite the kernel name with the BOOT_IMAGE= argument, and thus
1014 we have the final argument. */
1015 *argp = boot_image;
1017 if (find_boolean(argp, "quiet"))
1018 opt_quiet = true;
1020 if (!opt_quiet)
1021 printf("Loading %s... ", kernel_name);
1022 errno = 0;
1023 if (loadfile(bestextfilename(kernel_name), &kernel_data, &kernel_len)) {
1024 if (opt_quiet)
1025 printf("Loading %s ", kernel_name);
1026 printf("failed: ");
1027 goto bail;
1028 }
1029 if (!opt_quiet)
1030 printf("ok\n");
1032 errno = 0;
1033 cmdline = make_cmdline(argp);
1034 if (!cmdline) {
1035 errmsg = "make_cmdline() failed: ";
1036 goto bailmsg;
1037 }
1039 /* Initialize the initramfs chain */
1040 errno = 0;
1041 initramfs = initramfs_init();
1042 if (!initramfs) {
1043 errmsg = "initramfs_init() failed: ";
1044 goto bailmsg;
1045 }
1047 if ((arg = find_argument(argp, "initrd="))) {
1048 while (1) {
1049 p = strchr(arg, ',');
1050 if (p)
1051 *p = '\0';
1053 initrd_name = arg;
1054 if (!opt_quiet)
1055 printf("Loading %s... ", initrd_name);
1056 errno = 0;
1057 if (initramfs_load_archive(initramfs, bestextfilename(initrd_name))) {
1058 if (opt_quiet)
1059 printf("Loading %s ", initrd_name);
1060 printf("failed: ");
1061 goto bail;
1062 }
1063 if (!opt_quiet)
1064 printf("ok\n");
1066 if (!p)
1067 break;
1069 *p++ = ',';
1070 arg = p;
1071 }
1072 }
1074 /* Append the DHCP info */
1075 if (opt_dhcpinfo &&
1076 !pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
1077 errno = 0;
1078 if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
1079 "/dhcpinfo.dat", 0, 0755)) {
1080 errmsg = "Unable to add DHCP info: ";
1081 goto bailmsg;
1082 }
1083 }
1085 if (has_custom_config() && custom_initrdlen) {
1086 void *data;
1088 if (!opt_quiet)
1089 printf("Loading custom initrd... ");
1090 if (loadcustominitrd(&data))
1091 initramfs_add_data(initramfs, data, custom_initrdlen, custom_initrdlen, 4);
1092 }
1094 /* Handle dtb and eventually other setup data */
1095 setup_data = setup_data_init();
1096 if (!setup_data)
1097 goto bail;
1099 for (argl = argv; (arg = *argl); argl++) {
1100 if (!memcmp(arg, "dtb=", 4)) {
1101 if (setup_data_file(setup_data, SETUP_DTB, arg+4, opt_quiet))
1102 goto bail;
1103 } else if (!memcmp(arg, "blob.", 5)) {
1104 uint32_t type;
1105 char *ep;
1107 type = strtoul(arg + 5, &ep, 10);
1108 if (!type || ep[0] != '=' || !ep[1])
1109 continue;
1111 if (setup_data_file(setup_data, type, ep+1, opt_quiet))
1112 goto bail;
1113 }
1114 }
1116 /* This should not return... */
1117 errno = 0;
1118 syslinux_boot_linux(kernel_data, kernel_len, initramfs,
1119 setup_data, cmdline);
1120 errmsg = "syslinux_boot_linux() failed: ";
1122 bailmsg:
1123 fprintf(stderr, errmsg);
1124 bail:
1125 errmsg = "Error %d";
1126 switch(errno) {
1127 case ENOENT:
1128 errmsg = "File not found";
1129 break;
1130 case ENOMEM:
1131 errmsg = "Out of memory";
1132 }
1133 fprintf(stderr, errmsg, errno);
1134 fprintf(stderr, "\n%luM RAM and %s bit cpu found.\n%s: Boot aborted!\n",
1135 memory_size() >> 10,
1136 cpu_has_feature(X86_FEATURE_LM) ? "64": "32",
1137 progname);
1138 return 1;
1139 }
1141 #ifdef EXTRA
1142 static int main_setarg(int argc, char *argv[])
1143 {
1144 if (argc < 3) {
1145 usage("Usage: setarg.c32 argnum [args]...");
1146 }
1147 setlinuxarg(atoi(argv[1]), argc - 2, argv + 2);
1148 return 0;
1149 }
1151 static int main_ifarg(int argc, char *argv[])
1152 {
1153 int i;
1154 size_t size;
1156 if (argc < 3) {
1157 usage("Usage: ifarg.c32 [argnum labelifset]... labelifnoneset");
1158 }
1159 for (i = 1; i < argc - 1; i += 2) {
1160 int n = atoi(argv[i]);
1161 if (n == -1) {
1162 for (n = 0; n < 255; n++) {
1163 if (syslinux_getadv(n, &size))
1164 goto found;
1165 }
1166 continue;
1167 }
1168 else if (! syslinux_getadv(n, &size)) continue;
1169 found:
1170 syslinux_run_command(argv[i+1]);
1171 }
1172 if (i != argc) syslinux_run_command(argv[i]);
1173 else syslinux_run_default();
1174 return 0;
1175 }
1177 /* ----------------------------------------------------------------------- *
1178 *
1179 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
1180 *
1181 * This program is free software; you can redistribute it and/or modify
1182 * it under the terms of the GNU General Public License as published by
1183 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
1184 * Boston MA 02111-1307, USA; either version 2 of the License, or
1185 * (at your option) any later version; incorporated herein by reference.
1186 *
1187 * ----------------------------------------------------------------------- */
1189 static int main_listarg(int argc, char *argv[])
1190 {
1191 uint8_t *p, *ep;
1192 size_t s = syslinux_adv_size();
1193 char buf[256];
1195 (void) argc;
1196 (void) argv;
1197 p = syslinux_adv_ptr();
1199 printf("args size: %zd bytes at %p\n", s, p);
1201 ep = p + s; /* Need at least opcode+len */
1202 while (p < ep - 1 && *p) {
1203 int t = *p++;
1204 int l = *p++;
1206 if (p + l > ep)
1207 break;
1209 memcpy(buf, p, l);
1210 buf[l] = '\0';
1212 printf("arg %3d: \"%s\"\n", t, buf);
1214 p += l;
1215 }
1216 sleep(5);
1217 return 0;
1218 }
1219 #endif
1221 int main(int argc, char *argv[])
1222 {
1223 unsigned i;
1224 static struct {
1225 char *name;
1226 int (*main)(int argc, char *argv[]);
1227 } bin[] = {
1228 #ifdef EXTRA
1229 { "say", main_say },
1230 { "setarg", main_setarg },
1231 { "ifarg", main_ifarg },
1232 { "listarg", main_listarg },
1233 #endif
1234 { "kbdmap", main_kbdmap },
1235 { "kbd", main_kbdmap },
1236 { "ifmem", main_ifmem },
1237 { "linux", main_linux },
1238 { "md5sum", main_md5sum },
1239 { "reboot", main_reboot },
1240 { "poweroff", main_poweroff }
1241 };
1243 openconsole(&dev_null_r, &dev_stdcon_w);
1245 if (strstr(argv[0], "c32box")) { argc--; argv++; }
1246 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1247 if (strstr(argv[0], bin[i].name))
1248 return bin[i].main(argc, argv);
1249 printf("No %s in c32box modules\n", argv[0]);
1250 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1251 printf(" %s \n",bin[i].name);
1252 return 1;
1253 }