Merging upstream version 5.01+dfsg.
[syslinux-debian/hramrach.git] / com32 / elflink / ldlinux / execute.c
blob5c53b99507c36b13c1cd11a9bb9c689f71a484be
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdio.h>
16 #include <dprintf.h>
18 #include <com32.h>
19 #include <sys/exec.h>
20 #include <sys/io.h>
21 #include <sys/module.h>
22 #include "core.h"
23 #include "menu.h"
24 #include "fs.h"
25 #include "config.h"
26 #include "localboot.h"
27 #include "bios.h"
29 #include <syslinux/bootrm.h>
30 #include <syslinux/movebits.h>
31 #include <syslinux/config.h>
32 #include <syslinux/boot.h>
34 const struct image_types image_boot_types[] = {
35 { "localboot", IMAGE_TYPE_LOCALBOOT },
36 { "kernel", IMAGE_TYPE_KERNEL },
37 { "linux", IMAGE_TYPE_LINUX },
38 { "boot", IMAGE_TYPE_BOOT },
39 { "bss", IMAGE_TYPE_BSS },
40 { "pxe", IMAGE_TYPE_PXE },
41 { "fdimage", IMAGE_TYPE_FDIMAGE },
42 { "com32", IMAGE_TYPE_COM32 },
43 { "config", IMAGE_TYPE_CONFIG },
44 { NULL, 0 },
47 extern int create_args_and_load(char *);
49 __export void execute(const char *cmdline, uint32_t type)
51 const char *kernel, *args;
52 const char *p;
53 com32sys_t ireg;
54 char *q;
56 memset(&ireg, 0, sizeof ireg);
58 q = malloc(strlen(cmdline) + 2);
59 if (!q) {
60 printf("%s(): Fail to malloc a buffer to exec %s\n",
61 __func__, cmdline);
62 return;
65 kernel = q;
66 p = cmdline;
67 while (*p && !my_isspace(*p))
68 *q++ = *p++;
69 *q++ = '\0';
71 args = q;
72 while (*p && my_isspace(*p))
73 p++;
75 strcpy(q, p);
77 dprintf("kernel is %s, args = %s type = %d \n", kernel, args, type);
79 if (kernel[0] == '.') {
80 /* It might be a type specifier */
81 const struct image_types *t;
82 for (t = image_boot_types; t->name; t++) {
83 if (!strcmp(kernel + 1, t->name)) {
85 * Strip the type specifier, apply the
86 * filename extension if COM32 and
87 * retry.
89 if (t->type == IMAGE_TYPE_COM32) {
90 p = apply_extension(p, ".c32");
91 if (!p)
92 return;
95 execute(p, t->type);
96 return;
101 if (type == IMAGE_TYPE_COM32) {
103 * We may be called with the console in an unknown
104 * state, so initialise it.
106 ldlinux_console_init();
108 /* new entry for elf format c32 */
109 if (create_args_and_load((char *)cmdline))
110 printf("Failed to load COM32 file %s\n", kernel);
113 * The old COM32 module code would run the module then
114 * drop the user back at the command prompt,
115 * irrespective of how the COM32 module was loaded,
116 * e.g. from vesamenu.c32.
118 unload_modules_since("ldlinux.c32");
120 /* Restore the console */
121 ldlinux_console_init();
123 ldlinux_enter_command();
124 } else if (type == IMAGE_TYPE_CONFIG) {
125 char *argv[] = { "ldlinux.c32", NULL };
126 int rv;
128 /* kernel contains the config file name */
129 realpath(ConfigName, kernel, FILENAME_MAX);
131 /* If we got anything on the command line, do a chdir */
132 if (*args)
133 mangle_name(config_cwd, args);
135 rv = start_ldlinux(argv);
136 printf("Failed to exec ldlinux.c32: %s\n", strerror(rv));
137 } else if (type == IMAGE_TYPE_LOCALBOOT) {
138 local_boot(strtoul(kernel, NULL, 0));
139 } else if (type == IMAGE_TYPE_PXE || type == IMAGE_TYPE_BSS ||
140 type == IMAGE_TYPE_BOOT) {
141 chainboot_file(kernel, type);
142 } else {
143 /* Need add one item for kernel load, as we don't use
144 * the assembly runkernel.inc any more */
145 new_linux_kernel((char *)kernel, (char *)cmdline);
148 free((void *)kernel);
150 /* If this returns, something went bad; return to menu */