~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to loader/i386/ieee1275/linux.c

Tags: upstream-1.99~20101122
ImportĀ upstreamĀ versionĀ 1.99~20101122

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* linux.c - boot Linux zImage or bzImage */
2
 
/*
3
 
 *  GRUB  --  GRand Unified Bootloader
4
 
 *  Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009,2010  Free Software Foundation, Inc.
5
 
 *
6
 
 *  GRUB is free software: you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation, either version 3 of the License, or
9
 
 *  (at your option) any later version.
10
 
 *
11
 
 *  GRUB is distributed in the hope that it will be useful,
12
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 *  GNU General Public License for more details.
15
 
 *
16
 
 *  You should have received a copy of the GNU General Public License
17
 
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
#include <grub/loader.h>
21
 
#include <grub/machine/loader.h>
22
 
#include <grub/machine/memory.h>
23
 
#include <grub/file.h>
24
 
#include <grub/err.h>
25
 
#include <grub/disk.h>
26
 
#include <grub/misc.h>
27
 
#include <grub/types.h>
28
 
#include <grub/mm.h>
29
 
#include <grub/dl.h>
30
 
#include <grub/env.h>
31
 
#include <grub/term.h>
32
 
#include <grub/cpu/linux.h>
33
 
#include <grub/ieee1275/ieee1275.h>
34
 
#include <grub/command.h>
35
 
#include <grub/i18n.h>
36
 
 
37
 
#define GRUB_OFW_LINUX_PARAMS_ADDR      0x90000
38
 
#define GRUB_OFW_LINUX_KERNEL_ADDR      0x100000
39
 
#define GRUB_OFW_LINUX_INITRD_ADDR      0x800000
40
 
 
41
 
#define GRUB_OFW_LINUX_CL_OFFSET        0x1e00
42
 
#define GRUB_OFW_LINUX_CL_LENGTH        0x100
43
 
 
44
 
static grub_dl_t my_mod;
45
 
 
46
 
static grub_size_t kernel_size;
47
 
static char *kernel_addr, *kernel_cmdline;
48
 
static grub_size_t initrd_size;
49
 
 
50
 
static grub_err_t
51
 
grub_linux_unload (void)
52
 
{
53
 
  grub_free (kernel_cmdline);
54
 
  grub_free (kernel_addr);
55
 
  kernel_cmdline = 0;
56
 
  kernel_addr = 0;
57
 
  initrd_size = 0;
58
 
 
59
 
  grub_dl_unref (my_mod);
60
 
 
61
 
  return GRUB_ERR_NONE;
62
 
}
63
 
 
64
 
/*
65
 
static int
66
 
grub_ieee1275_debug (void)
67
 
{
68
 
  struct enter_args
69
 
  {
70
 
    struct grub_ieee1275_common_hdr common;
71
 
  }
72
 
  args;
73
 
 
74
 
  INIT_IEEE1275_COMMON (&args.common, "enter", 0, 0);
75
 
 
76
 
  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
77
 
    return -1;
78
 
 
79
 
  return 0;
80
 
}
81
 
*/
82
 
 
83
 
static grub_err_t
84
 
grub_linux_boot (void)
85
 
{
86
 
  struct linux_kernel_params *params;
87
 
  struct linux_kernel_header *lh;
88
 
  char *prot_code;
89
 
  char *bootpath;
90
 
  grub_ssize_t len;
91
 
 
92
 
  bootpath = grub_env_get ("root");
93
 
  if (bootpath)
94
 
    grub_ieee1275_set_property (grub_ieee1275_chosen,
95
 
                                "bootpath", bootpath,
96
 
                                grub_strlen (bootpath) + 1,
97
 
                                &len);
98
 
 
99
 
  params = (struct linux_kernel_params *) GRUB_OFW_LINUX_PARAMS_ADDR;
100
 
  lh = (struct linux_kernel_header *) params;
101
 
 
102
 
  grub_memset ((char *) params, 0, GRUB_OFW_LINUX_CL_OFFSET);
103
 
 
104
 
  params->alt_mem = grub_mmap_get_upper () >> 10;
105
 
  params->ext_mem = params->alt_mem;
106
 
 
107
 
  lh->cmd_line_ptr = (char *)
108
 
        (GRUB_OFW_LINUX_PARAMS_ADDR + GRUB_OFW_LINUX_CL_OFFSET);
109
 
 
110
 
  params->cl_magic = GRUB_LINUX_CL_MAGIC;
111
 
  params->cl_offset = GRUB_OFW_LINUX_CL_OFFSET;
112
 
 
113
 
  {
114
 
    grub_term_output_t term;
115
 
    int found = 0;
116
 
    FOR_ACTIVE_TERM_OUTPUTS(term)
117
 
      if (grub_strcmp (term->name, "ofconsole") == 0)
118
 
        {
119
 
          grub_uint16_t pos = grub_term_getxy (term);
120
 
          params->video_cursor_x = pos >> 8;
121
 
          params->video_cursor_y = pos & 0xff;
122
 
          params->video_width = grub_term_width (term);
123
 
          params->video_height = grub_term_height (term);
124
 
          found = 1;
125
 
          break;
126
 
        }
127
 
    if (!found)
128
 
      {
129
 
        params->video_cursor_x = 0;
130
 
        params->video_cursor_y = 0;
131
 
        params->video_width = 80;
132
 
        params->video_height = 25;
133
 
      }
134
 
  }
135
 
 
136
 
  params->font_size = 16;
137
 
 
138
 
  params->ofw_signature = GRUB_LINUX_OFW_SIGNATURE;
139
 
  params->ofw_num_items = 1;
140
 
  params->ofw_cif_handler = (grub_uint32_t) grub_ieee1275_entry_fn;
141
 
  params->ofw_idt = 0;
142
 
 
143
 
  if (initrd_size)
144
 
    {
145
 
      lh->type_of_loader = 1;
146
 
      lh->ramdisk_image = GRUB_OFW_LINUX_INITRD_ADDR;
147
 
      lh->ramdisk_size = initrd_size;
148
 
    }
149
 
 
150
 
  if (kernel_cmdline)
151
 
    grub_strcpy (lh->cmd_line_ptr, kernel_cmdline);
152
 
 
153
 
  prot_code = (char *) GRUB_OFW_LINUX_KERNEL_ADDR;
154
 
  grub_memcpy (prot_code, kernel_addr, kernel_size);
155
 
 
156
 
  asm volatile ("movl %0, %%esi" : : "m" (params));
157
 
  asm volatile ("movl %%esi, %%esp" : : );
158
 
  asm volatile ("movl %0, %%ecx" : : "m" (prot_code));
159
 
  asm volatile ("xorl %%ebx, %%ebx" : : );
160
 
  asm volatile ("jmp *%%ecx" : : );
161
 
 
162
 
  return GRUB_ERR_NONE;
163
 
}
164
 
 
165
 
static grub_err_t
166
 
grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)),
167
 
                int argc, char *argv[])
168
 
{
169
 
  grub_file_t file = 0;
170
 
  struct linux_kernel_header lh;
171
 
  grub_uint8_t setup_sects;
172
 
  grub_size_t real_size, prot_size;
173
 
  int i;
174
 
  char *dest;
175
 
 
176
 
  grub_dl_ref (my_mod);
177
 
 
178
 
  if (argc == 0)
179
 
    {
180
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "no kernel specified");
181
 
      goto fail;
182
 
    }
183
 
 
184
 
  file = grub_file_open (argv[0]);
185
 
  if (! file)
186
 
    goto fail;
187
 
 
188
 
  if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh))
189
 
    {
190
 
      grub_error (GRUB_ERR_READ_ERROR, "cannot read the Linux header");
191
 
      goto fail;
192
 
    }
193
 
 
194
 
  if ((lh.boot_flag != grub_cpu_to_le16 (0xaa55)) ||
195
 
      (lh.header != grub_cpu_to_le32 (GRUB_LINUX_MAGIC_SIGNATURE)))
196
 
    {
197
 
      grub_error (GRUB_ERR_BAD_OS, "invalid magic number");
198
 
      goto fail;
199
 
    }
200
 
 
201
 
  setup_sects = lh.setup_sects;
202
 
  if (! setup_sects)
203
 
    setup_sects = GRUB_LINUX_DEFAULT_SETUP_SECTS;
204
 
 
205
 
  real_size = setup_sects << GRUB_DISK_SECTOR_BITS;
206
 
  prot_size = grub_file_size (file) - real_size - GRUB_DISK_SECTOR_SIZE;
207
 
 
208
 
  grub_printf ("   [Linux-%s, setup=0x%x, size=0x%x]\n",
209
 
               "bzImage", real_size, prot_size);
210
 
 
211
 
  grub_file_seek (file, real_size + GRUB_DISK_SECTOR_SIZE);
212
 
  if (grub_errno)
213
 
    goto fail;
214
 
 
215
 
  kernel_cmdline = grub_malloc (GRUB_OFW_LINUX_CL_LENGTH);
216
 
  if (! kernel_cmdline)
217
 
    goto fail;
218
 
 
219
 
  dest = kernel_cmdline;
220
 
  for (i = 1;
221
 
       i < argc
222
 
       && dest + grub_strlen (argv[i]) + 1 < (kernel_cmdline
223
 
                                              + GRUB_OFW_LINUX_CL_LENGTH);
224
 
       i++)
225
 
    {
226
 
      *dest++ = ' ';
227
 
      dest = grub_stpcpy (dest, argv[i]);
228
 
    }
229
 
 
230
 
  kernel_addr = grub_malloc (prot_size);
231
 
  if (! kernel_addr)
232
 
    goto fail;
233
 
 
234
 
  kernel_size = prot_size;
235
 
  if (grub_file_read (file, kernel_addr, prot_size) != (int) prot_size)
236
 
    grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
237
 
 
238
 
  if (grub_errno == GRUB_ERR_NONE)
239
 
    grub_loader_set (grub_linux_boot, grub_linux_unload, 1);
240
 
 
241
 
fail:
242
 
 
243
 
  if (file)
244
 
    grub_file_close (file);
245
 
 
246
 
  if (grub_errno != GRUB_ERR_NONE)
247
 
    {
248
 
      grub_free (kernel_cmdline);
249
 
      grub_free (kernel_addr);
250
 
      kernel_cmdline = 0;
251
 
      kernel_addr = 0;
252
 
 
253
 
      grub_dl_unref (my_mod);
254
 
    }
255
 
 
256
 
  return grub_errno;
257
 
}
258
 
 
259
 
static grub_err_t
260
 
grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)),
261
 
                 int argc, char *argv[])
262
 
{
263
 
  grub_file_t file = 0;
264
 
 
265
 
  if (argc == 0)
266
 
    {
267
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
268
 
      goto fail;
269
 
    }
270
 
 
271
 
  if (! kernel_addr)
272
 
    {
273
 
      grub_error (GRUB_ERR_BAD_ARGUMENT, "you need to load the kernel first");
274
 
      goto fail;
275
 
    }
276
 
 
277
 
  file = grub_file_open (argv[0]);
278
 
  if (! file)
279
 
    goto fail;
280
 
 
281
 
  initrd_size = grub_file_size (file);
282
 
  if (grub_file_read (file, (void *) GRUB_OFW_LINUX_INITRD_ADDR,
283
 
                      initrd_size) != (int) initrd_size)
284
 
    {
285
 
      grub_error (GRUB_ERR_FILE_READ_ERROR, "couldn't read file");
286
 
      goto fail;
287
 
    }
288
 
 
289
 
fail:
290
 
  if (file)
291
 
    grub_file_close (file);
292
 
 
293
 
  return grub_errno;
294
 
}
295
 
 
296
 
static grub_command_t cmd_linux, cmd_initrd;
297
 
 
298
 
GRUB_MOD_INIT(linux)
299
 
{
300
 
  cmd_linux = grub_register_command ("linux", grub_cmd_linux,
301
 
                                     0, N_("Load Linux."));
302
 
  cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd,
303
 
                                      0, N_("Load initrd."));
304
 
  my_mod = mod;
305
 
}
306
 
 
307
 
GRUB_MOD_FINI(linux)
308
 
{
309
 
  grub_unregister_command (cmd_linux);
310
 
  grub_unregister_command (cmd_initrd);
311
 
}