wok-current view syslinux/stuff/extra/md5sum.c @ rev 23329
updated perl-file-desktopentry (0.04 -> 0.22)
author | Hans-G?nter Theisgen |
---|---|
date | Mon Mar 30 17:44:58 2020 +0100 (2020-03-30) |
parents | f063a9bbc7f4 |
children |
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));
631 if (p) {
632 void *heap = malloc(2*(1846 + (768 << (3 + 0))) + 16);
633 unlzma(kbdmap, p, heap);
634 free(heap);
635 free(kbdmap);
636 kbdmap = p;
637 }
638 }
639 if (strncmp(kbdmap, "07070", 5))
640 goto kbdmap_error;
642 // search for mapfile in cpio archive
643 for (i = 0; i < map_size;) {
644 int len, j;
645 char *name;
647 for (j = size = 0; j < 8; j++) {
648 char c = kbdmap[54 + i + j] - '0';
649 if (c > 9) c += '0' + 10 - 'A';
650 size <<= 4;
651 size += c;
652 }
653 i += 110;
654 name = kbdmap + i;
655 len = 1 + strlen(name);
656 i += len;
657 i += ((-i)&3);
658 if (!strcmp(name, kbdname)) {
659 kbdmap += i;
660 break;
661 }
662 i += size + ((-size)&3);
663 }
665 msg = "Filename error";
666 if (i >= map_size)
667 goto kbdmap_error;
669 msg = "Format error";
670 if (size != kmap->length)
671 goto kbdmap_error;
673 memcpy(kmap->map, kbdmap, size);
675 return 0;
677 kbdmap_error:
678 printf("%s.\n",msg);
679 return 1;
680 }
682 /* ----------------------------------------------------------------------- *
683 *
684 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
685 * Copyright 2009-2012 Intel Corporation; author: H. Peter Anvin
686 *
687 * Permission is hereby granted, free of charge, to any person
688 * obtaining a copy of this software and associated documentation
689 * files (the "Software"), to deal in the Software without
690 * restriction, including without limitation the rights to use,
691 * copy, modify, merge, publish, distribute, sublicense, and/or
692 * sell copies of the Software, and to permit persons to whom
693 * the Software is furnished to do so, subject to the following
694 * conditions:
695 *
696 * The above copyright notice and this permission notice shall
697 * be included in all copies or substantial portions of the Software.
698 *
699 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
700 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
701 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
702 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
703 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
704 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
705 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
706 * OTHER DEALINGS IN THE SOFTWARE.
707 *
708 * ----------------------------------------------------------------------- */
710 /*
711 * linux.c
712 *
713 * Sample module to load Linux kernels. This module can also create
714 * a file out of the DHCP return data if running under PXELINUX.
715 *
716 * If -dhcpinfo is specified, the DHCP info is written into the file
717 * /dhcpinfo.dat in the initramfs.
718 *
719 * Usage: linux.c32 [-dhcpinfo] kernel arguments...
720 */
722 #include <errno.h>
723 #include <stdbool.h>
724 #include <stdlib.h>
725 #include <stdio.h>
726 #include <string.h>
727 #include <console.h>
728 #include <cpuid.h>
729 #include <syslinux/loadfile.h>
730 #include <syslinux/linux.h>
731 #include <syslinux/pxe.h>
733 const char *progname = "c32box.c32";
735 /* Find the last instance of a particular command line argument
736 (which should include the final =; do not use for boolean arguments) */
737 static char *find_argument(char **argv, const char *argument)
738 {
739 int la = strlen(argument);
740 char **arg;
741 char *ptr = NULL;
743 for (arg = argv; *arg; arg++) {
744 if (!memcmp(*arg, argument, la))
745 ptr = *arg + la;
746 }
748 return ptr;
749 }
751 /* Search for a boolean argument; return its position, or 0 if not present */
752 #if 1
753 #define find_boolean(a,b) (find_argument(a,b) != NULL)
754 #else
755 static int find_boolean(char **argv, const char *argument)
756 {
757 char **arg;
759 for (arg = argv; *arg; arg++) {
760 if (!strcmp(*arg, argument))
761 return (arg - argv) + 1;
762 }
764 return 0;
765 }
766 #endif
768 static int got_config;
769 static char *custom_cmdline = "";
770 static int custom_initrdlen;
771 static int custom_initrdbase;
772 static char *custom_buffer;
773 static struct disk_info diskinfo;
775 static int has_custom_config(void)
776 {
777 const union syslinux_derivative_info *sdi;
779 if (got_config)
780 goto done;
781 got_config = -1;
782 sdi = syslinux_derivative_info();
783 if (sdi->c.filesystem != SYSLINUX_FS_ISOLINUX)
784 goto fail;
785 disk_get_params(sdi->iso.drive_number, &diskinfo);
786 custom_buffer = disk_read_sectors(&diskinfo, 32768 / diskinfo.bps, 1);
787 got_config = (*(unsigned long *) (custom_buffer + 80))
788 * 2048 / diskinfo.bps;
789 free(custom_buffer);
790 custom_buffer = disk_read_sectors(&diskinfo, got_config, 1);
791 if (!memcmp(custom_buffer,"#!boot ",7)) {
792 char *p = custom_buffer+7+32+1;
794 if (!memcmp(p,"append=",7)) {
795 custom_cmdline = p + 7;
796 p = strchr(p,'\n');
797 *p++ = 0;
798 }
799 if (!memcmp(p,"initrd:",7)) {
800 custom_initrdlen = strtoul(p + 7, &custom_initrdbase, 10);
801 custom_initrdbase += (got_config << 11) + 1 - (int) custom_buffer;
802 }
803 }
804 fail:
805 done:
806 return got_config > 0;
807 }
809 static int loadcustominitrd(void **data)
810 {
811 int n, len;
812 char *p;
814 p = *data = malloc(custom_initrdlen);
815 if (!p) return 0;
816 for (len = custom_initrdlen; len != 0; len -= n, p += n) {
817 free(custom_buffer);
818 custom_buffer = disk_read_sectors(&diskinfo, custom_initrdbase >> 11,
819 2048 / diskinfo.bps);
820 n = 2048 - (custom_initrdbase & 2047);
821 if (n > len)
822 n = len;
823 memcpy(p, custom_buffer + (custom_initrdbase & 2047), n);
824 custom_initrdbase += n;
825 }
826 return 1;
827 }
829 /* Stitch together the command line from a set of argv's */
830 static char *make_cmdline(char **argv)
831 {
832 char **arg;
833 size_t bytes, size;
834 char *cmdline, *p;
835 int i;
837 bytes = 1; /* Just in case we have a zero-entry cmdline */
838 for (arg = argv; *arg; arg++) {
839 bytes += strlen(*arg) + 1;
840 }
841 for (i = 0; i < 255; i++)
842 if (syslinux_getadv(i, &size))
843 bytes += ++size;
844 if (has_custom_config())
845 bytes += strlen(custom_cmdline) + 1;
847 p = cmdline = malloc(bytes);
848 if (!cmdline)
849 return NULL;
851 for (arg = argv; *arg; arg++) {
852 size = strlen(*arg);
853 memcpy(p, *arg, size);
854 p[size] = ' ';
855 p += size + 1;
856 }
858 for (i = 0; i < 255; i++) {
859 const void *q = syslinux_getadv(i, &size);
860 if (q == NULL) continue;
861 memcpy(p, q, size);
862 p[size] = ' ';
863 p += size + 1;
864 }
866 if (p > cmdline)
867 p--; /* Remove the last space */
868 *p = '\0';
870 if (has_custom_config()) {
871 *p++ = ' ';
872 strcpy(p, custom_cmdline);
873 }
875 return cmdline;
876 }
878 #define cpu_has_cpuid() cpu_has_eflag(X86_EFLAGS_ID)
880 static bool __constfunc cpu_has_level(uint32_t level)
881 {
882 uint32_t group;
883 uint32_t limit;
885 if (!cpu_has_cpuid())
886 return false;
888 group = level & 0xffff0000;
889 limit = cpuid_eax(group);
891 if ((limit & 0xffff0000) != group)
892 return false;
894 if (level > limit)
895 return false;
897 return true;
898 }
900 /* This only supports feature groups 0 and 1, corresponding to the
901 Intel and AMD EDX bit vectors. We can add more later if need be. */
902 static bool __constfunc cpu_has_feature(int x)
903 {
904 uint32_t level = ((x & 1) << 31) | 1;
906 return cpu_has_level(level) && ((cpuid_edx(level) >> (x & 31) & 1));
907 }
909 static const char *extfilename(const char *filename, char *ext, int feature)
910 {
911 #define NEWFILENAMESZ 256
912 static char newfilename[NEWFILENAMESZ+1];
913 const char *found = filename;
914 char *new = newfilename;
915 int fd;
917 if (strlen(filename) + strlen(ext) <= NEWFILENAMESZ) {
918 strcpy(newfilename, filename);
919 if (cpu_has_feature(feature)) {
920 strcat(newfilename, ext);
921 fd = open(new, O_RDONLY);
922 #ifdef WITH_UNROCKRIDGE
923 if (fd < 0)
924 fd = open(new = unrockridge(new), O_RDONLY);
925 #endif
926 if (fd >= 0) {
927 found = new;
928 close(fd);
929 }
930 }
931 }
932 return found;
933 }
935 static const char *bestextfilename(const char *filename)
936 {
937 const char *found;
939 //found = extfilename(filename, "fpu", X86_FEATURE_FPU);
940 //found = extfilename(filename, "686", X86_FEATURE_CMOV);
941 //found = extfilename(filename, "pae", X86_FEATURE_PAE);
942 found = extfilename(filename, "64", X86_FEATURE_LM);
943 //found = extfilename(filename, "guest", X86_FEATURE_HYPERVISOR);
944 return found;
945 }
947 static int setup_data_file(struct setup_data *setup_data,
948 uint32_t type, const char *filename,
949 bool opt_quiet)
950 {
951 if (!opt_quiet)
952 printf("Loading %s... ", filename);
954 if (setup_data_load(setup_data, type, filename)) {
955 if (opt_quiet)
956 printf("Loading %s ", filename);
957 printf("failed\n");
958 return -1;
959 }
961 if (!opt_quiet)
962 printf("ok\n");
964 return 0;
965 }
967 static int main_linux(int argc, char *argv[])
968 {
969 const char *kernel_name;
970 const char *initrd_name;
971 struct initramfs *initramfs;
972 struct setup_data *setup_data;
973 char *errmsg;
974 char *cmdline;
975 char *boot_image;
976 void *kernel_data;
977 size_t kernel_len;
978 bool opt_dhcpinfo = false;
979 bool opt_quiet = false;
980 void *dhcpdata;
981 size_t dhcplen;
982 char **argp, **argl, *arg, *p;
984 openconsole(&dev_null_r, &dev_stdcon_w);
986 (void)argc;
988 for (argp = argv + 1; (arg = *argp) && arg[0] == '-'; argp++) {
989 if (!strcmp("-dhcpinfo", arg)) {
990 opt_dhcpinfo = true;
991 } else {
992 errmsg = "%s: unknown option: %s\n";
993 goto unknown_option;
994 }
995 }
997 if (!arg) {
998 errmsg = "%s: missing kernel name\n";
999 unknown_option:
1000 fprintf(stderr, errmsg, progname, arg);
1001 return 1;
1002 }
1004 kernel_name = arg;
1006 errno = 0;
1007 boot_image = malloc(strlen(kernel_name) + 12);
1008 if (!boot_image) {
1009 errmsg = "Error allocating BOOT_IMAGE string: ";
1010 goto bailmsg;
1011 }
1012 strcpy(boot_image, "BOOT_IMAGE=");
1013 strcpy(boot_image + 11, kernel_name);
1014 /* argp now points to the kernel name, and the command line follows.
1015 Overwrite the kernel name with the BOOT_IMAGE= argument, and thus
1016 we have the final argument. */
1017 *argp = boot_image;
1019 if (find_boolean(argp, "quiet"))
1020 opt_quiet = true;
1022 if (!opt_quiet)
1023 printf("Loading %s... ", kernel_name);
1024 errno = 0;
1025 if (loadfile(bestextfilename(kernel_name), &kernel_data, &kernel_len)) {
1026 if (opt_quiet)
1027 printf("Loading %s ", kernel_name);
1028 printf("failed: ");
1029 goto bail;
1030 }
1031 if (!opt_quiet)
1032 printf("ok\n");
1034 errno = 0;
1035 cmdline = make_cmdline(argp);
1036 if (!cmdline) {
1037 errmsg = "make_cmdline() failed: ";
1038 goto bailmsg;
1039 }
1041 /* Initialize the initramfs chain */
1042 errno = 0;
1043 initramfs = initramfs_init();
1044 if (!initramfs) {
1045 errmsg = "initramfs_init() failed: ";
1046 goto bailmsg;
1047 }
1049 if ((arg = find_argument(argp, "initrd="))) {
1050 while (1) {
1051 p = strchr(arg, ',');
1052 if (p)
1053 *p = '\0';
1055 initrd_name = arg;
1056 if (!opt_quiet)
1057 printf("Loading %s... ", initrd_name);
1058 errno = 0;
1059 if (initramfs_load_archive(initramfs, bestextfilename(initrd_name))) {
1060 if (opt_quiet)
1061 printf("Loading %s ", initrd_name);
1062 printf("failed: ");
1063 goto bail;
1064 }
1065 if (!opt_quiet)
1066 printf("ok\n");
1068 if (!p)
1069 break;
1071 *p++ = ',';
1072 arg = p;
1073 }
1074 }
1076 /* Append the DHCP info */
1077 if (opt_dhcpinfo &&
1078 !pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK, &dhcpdata, &dhcplen)) {
1079 errno = 0;
1080 if (initramfs_add_file(initramfs, dhcpdata, dhcplen, dhcplen,
1081 "/dhcpinfo.dat", 0, 0755)) {
1082 errmsg = "Unable to add DHCP info: ";
1083 goto bailmsg;
1084 }
1085 }
1087 if (has_custom_config() && custom_initrdlen) {
1088 void *data;
1090 if (!opt_quiet)
1091 printf("Loading custom initrd... ");
1092 if (loadcustominitrd(&data))
1093 initramfs_add_data(initramfs, data, custom_initrdlen, custom_initrdlen, 4);
1094 }
1096 /* Handle dtb and eventually other setup data */
1097 setup_data = setup_data_init();
1098 if (!setup_data)
1099 goto bail;
1101 for (argl = argv; (arg = *argl); argl++) {
1102 if (!memcmp(arg, "dtb=", 4)) {
1103 if (setup_data_file(setup_data, SETUP_DTB, arg+4, opt_quiet))
1104 goto bail;
1105 } else if (!memcmp(arg, "blob.", 5)) {
1106 uint32_t type;
1107 char *ep;
1109 type = strtoul(arg + 5, &ep, 10);
1110 if (!type || ep[0] != '=' || !ep[1])
1111 continue;
1113 if (setup_data_file(setup_data, type, ep+1, opt_quiet))
1114 goto bail;
1115 }
1116 }
1118 /* This should not return... */
1119 errno = 0;
1120 syslinux_boot_linux(kernel_data, kernel_len, initramfs,
1121 setup_data, cmdline);
1122 errmsg = "syslinux_boot_linux() failed: ";
1124 bailmsg:
1125 fprintf(stderr, errmsg);
1126 bail:
1127 errmsg = "Error %d";
1128 switch(errno) {
1129 case ENOENT:
1130 errmsg = "File not found";
1131 break;
1132 case ENOMEM:
1133 errmsg = "Out of memory";
1134 }
1135 fprintf(stderr, errmsg, errno);
1136 fprintf(stderr, "\n%luM RAM and %s bit cpu found.\n%s: Boot aborted!\n",
1137 memory_size() >> 10,
1138 cpu_has_feature(X86_FEATURE_LM) ? "64": "32",
1139 progname);
1140 return 1;
1141 }
1143 #ifdef EXTRA
1144 static int main_setarg(int argc, char *argv[])
1145 {
1146 if (argc < 3) {
1147 usage("Usage: setarg.c32 argnum [args]...");
1148 }
1149 setlinuxarg(atoi(argv[1]), argc - 2, argv + 2);
1150 return 0;
1151 }
1153 static int main_ifarg(int argc, char *argv[])
1154 {
1155 int i;
1156 size_t size;
1158 if (argc < 3) {
1159 usage("Usage: ifarg.c32 [argnum labelifset]... labelifnoneset");
1160 }
1161 for (i = 1; i < argc - 1; i += 2) {
1162 int n = atoi(argv[i]);
1163 if (n == -1) {
1164 for (n = 0; n < 255; n++) {
1165 if (syslinux_getadv(n, &size))
1166 goto found;
1167 }
1168 continue;
1169 }
1170 else if (! syslinux_getadv(n, &size)) continue;
1171 found:
1172 syslinux_run_command(argv[i+1]);
1173 }
1174 if (i != argc) syslinux_run_command(argv[i]);
1175 else syslinux_run_default();
1176 return 0;
1177 }
1179 /* ----------------------------------------------------------------------- *
1180 *
1181 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
1182 *
1183 * This program is free software; you can redistribute it and/or modify
1184 * it under the terms of the GNU General Public License as published by
1185 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
1186 * Boston MA 02111-1307, USA; either version 2 of the License, or
1187 * (at your option) any later version; incorporated herein by reference.
1188 *
1189 * ----------------------------------------------------------------------- */
1191 static int main_listarg(int argc, char *argv[])
1192 {
1193 uint8_t *p, *ep;
1194 size_t s = syslinux_adv_size();
1195 char buf[256];
1197 (void) argc;
1198 (void) argv;
1199 p = syslinux_adv_ptr();
1201 printf("args size: %zd bytes at %p\n", s, p);
1203 ep = p + s; /* Need at least opcode+len */
1204 while (p < ep - 1 && *p) {
1205 int t = *p++;
1206 int l = *p++;
1208 if (p + l > ep)
1209 break;
1211 memcpy(buf, p, l);
1212 buf[l] = '\0';
1214 printf("arg %3d: \"%s\"\n", t, buf);
1216 p += l;
1217 }
1218 sleep(5);
1219 return 0;
1220 }
1221 #endif
1223 int main(int argc, char *argv[])
1224 {
1225 unsigned i;
1226 static struct {
1227 char *name;
1228 int (*main)(int argc, char *argv[]);
1229 } bin[] = {
1230 #ifdef EXTRA
1231 { "say", main_say },
1232 { "setarg", main_setarg },
1233 { "ifarg", main_ifarg },
1234 { "listarg", main_listarg },
1235 #endif
1236 { "kbdmap", main_kbdmap },
1237 { "kbd", main_kbdmap },
1238 { "ifmem", main_ifmem },
1239 { "linux", main_linux },
1240 { "md5sum", main_md5sum },
1241 { "reboot", main_reboot },
1242 { "poweroff", main_poweroff }
1243 };
1245 openconsole(&dev_null_r, &dev_stdcon_w);
1247 if (strstr(argv[0], "c32box")) { argc--; argv++; }
1248 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1249 if (strstr(argv[0], bin[i].name))
1250 return bin[i].main(argc, argv);
1251 printf("No %s in c32box modules\n", argv[0]);
1252 for (i = 0; i < sizeof(bin)/sizeof(bin[0]); i++)
1253 printf(" %s \n",bin[i].name);
1254 return 1;
1255 }