summaryrefslogtreecommitdiff
path: root/com32/lua/src/syslinux.c
blob: cafa82548249b89a41e46d19e6c824d8ab5c21ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include <stdlib.h>
#include <string.h>

#define lnetlib_c       /* Define the library */

#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "syslinux/boot.h"
#include "syslinux/loadfile.h"
#include "syslinux/linux.h"
#include "../../../menu/libmenu/com32io.h"

int __parse_argv(char ***argv, const char *str);

/*
 * Most code taken from:
 *	com32/modules/linux.c
 */

/* Find the last instance of a particular command line argument
   (which should include the final =; do not use for boolean arguments) */
static char *find_argument(char **argv, const char *argument)
{
  int la = strlen(argument);
  char **arg;
  char *ptr = NULL;

  for (arg = argv; *arg; arg++) {
    if (!memcmp(*arg, argument, la))
      ptr = *arg + la;
  }

  return ptr;
}

/* Get a value with a potential suffix (k/m/g/t/p/e) */
static unsigned long long suffix_number(const char *str)
{
  char *ep;
  unsigned long long v;
  int shift;

  v = strtoull(str, &ep, 0);
  switch (*ep|0x20) {
  case 'k':
    shift = 10;
    break;
  case 'm':
    shift = 20;
    break;
  case 'g':
    shift = 30;
    break;
  case 't':
    shift = 40;
    break;
  case 'p':
    shift = 50;
    break;
  case 'e':
    shift = 60;
    break;
  default:
    shift = 0;
    break;
  }
  v <<= shift;

  return v;
}

/* Truncate to 32 bits, with saturate */
static inline uint32_t saturate32(unsigned long long v)
{
  return (v > 0xffffffff) ? 0xffffffff : (uint32_t)v;
}

/* Stitch together the command line from a set of argv's */
static char *make_cmdline(char **argv)
{
  char **arg;
  size_t bytes;
  char *cmdline, *p;

  bytes = 1;			/* Just in case we have a zero-entry cmdline */
  for (arg = argv; *arg; arg++) {
    bytes += strlen(*arg)+1;
  }

  p = cmdline = malloc(bytes);
  if (!cmdline)
    return NULL;

  for (arg = argv; *arg; arg++) {
    int len = strlen(*arg);
    memcpy(p, *arg, len);
    p[len] = ' ';
    p += len+1;
  }

  if (p > cmdline)
    p--;			/* Remove the last space */
  *p = '\0';

  return cmdline;
}


static int sl_run_command(lua_State *L)
{
  const char *cmd = luaL_checkstring(L, 1);  /* Reads the string parameter */
  syslinux_run_command(cmd);
  return 0;
}

/* do default boot */
static int sl_run_default(lua_State *L)
{
  syslinux_run_default();
  return 0;
}

/* do local boot */
static int sl_local_boot(lua_State *L)
{
  uint16_t flags = luaL_checkint(L, 1);
  syslinux_local_boot(flags);
  return 0;
}

static int sl_final_cleanup(lua_State *L)
{
  uint16_t flags = luaL_checkint(L, 1);
  syslinux_local_boot(flags);
  return 0;
}

/* boot linux kernel and initrd */
static int sl_boot_linux(lua_State *L)
{
  const char *kernel = luaL_checkstring(L, 1);
  const char *cmdline = luaL_optstring(L, 2, "");
  char *initrd;
  void *kernel_data, *file_data;
  size_t kernel_len, file_len;
  struct initramfs *initramfs;
  char *newcmdline;
  uint32_t mem_limit = luaL_optint(L, 3, 0);
  uint16_t video_mode = luaL_optint(L, 4, 0);
  int ret, i;
  char **argv, **argp, *arg, *p;

  ret = __parse_argv(&argv, cmdline);

  newcmdline = malloc(strlen(kernel)+12);
  if (!newcmdline)
    printf("Mem alloc failed: cmdline\n");

  strcpy(newcmdline, "BOOT_IMAGE=");
  strcpy(newcmdline+strlen(newcmdline), kernel);
  argv[0] = newcmdline;
  argp = argv;

  /* DEBUG
  for (i=0; i<ret; i++)
    printf("%d: %s\n", i, argv[i]);
  */

  newcmdline = make_cmdline(argp);
  if (!newcmdline)
    printf("Creating command line failed!\n");

  /* DEBUG
  printf("Command line: %s\n", newcmdline);
  sleep(1000);
  */
  
  /* Look for specific command-line arguments we care about */
  if ((arg = find_argument(argp, "mem=")))
    mem_limit = saturate32(suffix_number(arg));

  if ((arg = find_argument(argp, "vga="))) {
    switch (arg[0] | 0x20) {
    case 'a':			/* "ask" */
      video_mode = 0xfffd;
      break;
    case 'e':			/* "ext" */
      video_mode = 0xfffe;
      break;
    case 'n':			/* "normal" */
      video_mode = 0xffff;
      break;
    default:
      video_mode = strtoul(arg, NULL, 0);
      break;
    }
  }
 

  printf("Loading kernel %s...\n", kernel);
  if (loadfile(kernel, &kernel_data, &kernel_len))
    printf("failed!\n");
  else printf("ok\n");

  initramfs = initramfs_init();
  if (!initramfs)
    printf("Initializing initrd failed!\n");

  if ((arg = find_argument(argp, "initrd="))) {
    do {
      p = strchr(arg, ',');
      if (p)
        *p = '\0';

      initrd = arg;
      printf("Loading initrd %s... ", initrd);
      if (initramfs_load_archive(initramfs, initrd)) {
        printf("failed!\n");
      }
      printf("ok\n");

      if (p)
        *p++ = ',';
    } while ((arg = p));
  }

  if (!loadfile("/testfile1", &file_data, &file_len)) {
    if (initramfs_add_file(initramfs, file_data, file_len, file_len,
                           "/testfile1", 0, 0755))
       printf("Adding extra file failed\n");
  } else printf("Loading extra file failed\n");

  sleep(10000);

  ret = syslinux_boot_linux(kernel_data, kernel_len, initramfs, newcmdline,
                      video_mode, mem_limit);
  printf("syslinux_boot_linux returned %d\n", ret);

  return 0;
}


/* sleep for msec milliseconds */
static int sl_sleep(lua_State *L)
{
  unsigned int msec = luaL_checkint(L, 1);
  sleep(msec);
  return 0;
}


static int sl_run_kernel_image(lua_State *L)
{
  const char *filename = luaL_checkstring(L, 1);
  const char *cmdline = luaL_checkstring(L, 2);
  uint32_t ipappend_flags = luaL_checkint(L, 3);
  uint32_t type = luaL_checkint(L, 4);

  syslinux_run_kernel_image(filename, cmdline, ipappend_flags, type);
  return 0;
}

static const luaL_reg syslinuxlib[] = {
  {"run_command", sl_run_command},
  {"run_default", sl_run_default},
  {"local_boot", sl_local_boot},
  {"final_cleanup", sl_final_cleanup},
  {"boot_linux", sl_boot_linux},
  {"run_kernel_image", sl_run_kernel_image},
  {"sleep", sl_sleep},
  {NULL, NULL}
};

/* This defines a function that opens up your library. */

LUALIB_API int luaopen_syslinux (lua_State *L) {
  luaL_openlib(L, LUA_SYSLINUXLIBNAME, syslinuxlib, 0);
  return 1;
}