]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * GRUB -- GRand Unified Bootloader | |
3 | * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc. | |
4 | * | |
5 | * GRUB 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, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * GRUB is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with GRUB. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include <grub/machine/memory.h> | |
20 | #include <grub/memory.h> | |
21 | #ifdef GRUB_MACHINE_PCBIOS | |
22 | #include <grub/machine/biosnum.h> | |
23 | #endif | |
24 | #include <grub/multiboot.h> | |
25 | #include <grub/cpu/multiboot.h> | |
26 | #include <grub/disk.h> | |
27 | #include <grub/device.h> | |
28 | #include <grub/partition.h> | |
29 | #include <grub/mm.h> | |
30 | #include <grub/misc.h> | |
31 | #include <grub/env.h> | |
32 | #include <grub/video.h> | |
33 | ||
34 | struct module | |
35 | { | |
36 | struct module *next; | |
37 | grub_addr_t start; | |
38 | grub_size_t size; | |
39 | char *cmdline; | |
40 | int cmdline_size; | |
41 | }; | |
42 | ||
43 | struct module *modules, *modules_last; | |
44 | static grub_size_t cmdline_size; | |
45 | static grub_size_t total_modcmd; | |
46 | static unsigned modcnt; | |
47 | static char *cmdline = NULL; | |
48 | static grub_uint32_t bootdev; | |
49 | static int bootdev_set; | |
50 | ||
51 | grub_size_t | |
52 | grub_multiboot_get_mbi_size (void) | |
53 | { | |
54 | return sizeof (struct multiboot_info) + ALIGN_UP (cmdline_size, 4) | |
55 | + modcnt * sizeof (struct multiboot_mod_list) + total_modcmd | |
56 | + ALIGN_UP (sizeof(PACKAGE_STRING), 4) | |
57 | + grub_get_multiboot_mmap_count () * sizeof (struct multiboot_mmap_entry) | |
58 | #if GRUB_MACHINE_HAS_VBE | |
59 | + sizeof (struct grub_vbe_info_block) | |
60 | + sizeof (struct grub_vbe_mode_info_block) | |
61 | #endif | |
62 | + 256 * sizeof (struct multiboot_color); | |
63 | } | |
64 | ||
65 | /* Fill previously allocated Multiboot mmap. */ | |
66 | static void | |
67 | grub_fill_multiboot_mmap (struct multiboot_mmap_entry *first_entry) | |
68 | { | |
69 | struct multiboot_mmap_entry *mmap_entry = (struct multiboot_mmap_entry *) first_entry; | |
70 | ||
71 | auto int NESTED_FUNC_ATTR hook (grub_uint64_t, grub_uint64_t, grub_uint32_t); | |
72 | int NESTED_FUNC_ATTR hook (grub_uint64_t addr, grub_uint64_t size, grub_uint32_t type) | |
73 | { | |
74 | mmap_entry->addr = addr; | |
75 | mmap_entry->len = size; | |
76 | switch (type) | |
77 | { | |
78 | case GRUB_MACHINE_MEMORY_AVAILABLE: | |
79 | mmap_entry->type = MULTIBOOT_MEMORY_AVAILABLE; | |
80 | break; | |
81 | ||
82 | #ifdef GRUB_MACHINE_MEMORY_ACPI_RECLAIMABLE | |
83 | case GRUB_MACHINE_MEMORY_ACPI_RECLAIMABLE: | |
84 | mmap_entry->type = MULTIBOOT_MEMORY_ACPI_RECLAIMABLE; | |
85 | break; | |
86 | #endif | |
87 | ||
88 | #ifdef GRUB_MACHINE_MEMORY_NVS | |
89 | case GRUB_MACHINE_MEMORY_NVS: | |
90 | mmap_entry->type = MULTIBOOT_MEMORY_NVS; | |
91 | break; | |
92 | #endif | |
93 | ||
94 | default: | |
95 | mmap_entry->type = MULTIBOOT_MEMORY_RESERVED; | |
96 | break; | |
97 | } | |
98 | mmap_entry->size = sizeof (struct multiboot_mmap_entry) - sizeof (mmap_entry->size); | |
99 | mmap_entry++; | |
100 | ||
101 | return 0; | |
102 | } | |
103 | ||
104 | grub_mmap_iterate (hook); | |
105 | } | |
106 | ||
107 | static grub_err_t | |
108 | retrieve_video_parameters (struct multiboot_info *mbi, | |
109 | grub_uint8_t *ptrorig, grub_uint32_t ptrdest) | |
110 | { | |
111 | grub_err_t err; | |
112 | struct grub_video_mode_info mode_info; | |
113 | void *framebuffer; | |
114 | grub_video_driver_id_t driv_id; | |
115 | struct grub_video_palette_data palette[256]; | |
116 | ||
117 | err = grub_multiboot_set_video_mode (); | |
118 | if (err) | |
119 | { | |
120 | grub_print_error (); | |
121 | grub_errno = GRUB_ERR_NONE; | |
122 | } | |
123 | ||
124 | grub_video_get_palette (0, ARRAY_SIZE (palette), palette); | |
125 | ||
126 | driv_id = grub_video_get_driver_id (); | |
127 | if (driv_id == GRUB_VIDEO_DRIVER_NONE) | |
128 | return GRUB_ERR_NONE; | |
129 | ||
130 | err = grub_video_get_info_and_fini (&mode_info, &framebuffer); | |
131 | if (err) | |
132 | return err; | |
133 | ||
134 | mbi->framebuffer_addr = (grub_addr_t) framebuffer; | |
135 | mbi->framebuffer_pitch = mode_info.pitch; | |
136 | ||
137 | mbi->framebuffer_width = mode_info.width; | |
138 | mbi->framebuffer_height = mode_info.height; | |
139 | ||
140 | mbi->framebuffer_bpp = mode_info.bpp; | |
141 | ||
142 | if (mode_info.mode_type & GRUB_VIDEO_MODE_TYPE_INDEX_COLOR) | |
143 | { | |
144 | struct multiboot_color *mb_palette; | |
145 | unsigned i; | |
146 | mbi->framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED; | |
147 | mbi->framebuffer_palette_addr = ptrdest; | |
148 | mbi->framebuffer_palette_num_colors = mode_info.number_of_colors; | |
149 | if (mbi->framebuffer_palette_num_colors > ARRAY_SIZE (palette)) | |
150 | mbi->framebuffer_palette_num_colors = ARRAY_SIZE (palette); | |
151 | mb_palette = (struct multiboot_color *) ptrorig; | |
152 | for (i = 0; i < mbi->framebuffer_palette_num_colors; i++) | |
153 | { | |
154 | mb_palette[i].red = palette[i].r; | |
155 | mb_palette[i].green = palette[i].g; | |
156 | mb_palette[i].blue = palette[i].b; | |
157 | } | |
158 | ptrorig += mbi->framebuffer_palette_num_colors | |
159 | * sizeof (struct multiboot_color); | |
160 | ptrdest += mbi->framebuffer_palette_num_colors | |
161 | * sizeof (struct multiboot_color); | |
162 | } | |
163 | else | |
164 | { | |
165 | mbi->framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_RGB; | |
166 | mbi->framebuffer_red_field_position = mode_info.green_field_pos; | |
167 | mbi->framebuffer_red_mask_size = mode_info.green_mask_size; | |
168 | mbi->framebuffer_green_field_position = mode_info.green_field_pos; | |
169 | mbi->framebuffer_green_mask_size = mode_info.green_mask_size; | |
170 | mbi->framebuffer_blue_field_position = mode_info.blue_field_pos; | |
171 | mbi->framebuffer_blue_mask_size = mode_info.blue_mask_size; | |
172 | } | |
173 | ||
174 | mbi->flags |= MULTIBOOT_INFO_FRAMEBUFFER_INFO; | |
175 | ||
176 | return GRUB_ERR_NONE; | |
177 | } | |
178 | ||
179 | grub_err_t | |
180 | grub_multiboot_make_mbi (void *orig, grub_uint32_t dest, grub_off_t buf_off, | |
181 | grub_size_t bufsize) | |
182 | { | |
183 | grub_uint8_t *ptrorig = (grub_uint8_t *) orig + buf_off; | |
184 | grub_uint32_t ptrdest = dest + buf_off; | |
185 | struct multiboot_info *mbi; | |
186 | struct multiboot_mod_list *modlist; | |
187 | unsigned i; | |
188 | struct module *cur; | |
189 | grub_size_t mmap_size; | |
190 | grub_err_t err; | |
191 | ||
192 | if (bufsize < grub_multiboot_get_mbi_size ()) | |
193 | return grub_error (GRUB_ERR_OUT_OF_MEMORY, "mbi buffer is too small"); | |
194 | ||
195 | mbi = (struct multiboot_info *) ptrorig; | |
196 | ptrorig += sizeof (*mbi); | |
197 | ptrdest += sizeof (*mbi); | |
198 | grub_memset (mbi, 0, sizeof (*mbi)); | |
199 | ||
200 | grub_memcpy (ptrorig, cmdline, cmdline_size); | |
201 | mbi->flags |= MULTIBOOT_INFO_CMDLINE; | |
202 | mbi->cmdline = ptrdest; | |
203 | ptrorig += ALIGN_UP (cmdline_size, 4); | |
204 | ptrdest += ALIGN_UP (cmdline_size, 4); | |
205 | ||
206 | grub_memcpy (ptrorig, PACKAGE_STRING, sizeof(PACKAGE_STRING)); | |
207 | mbi->flags |= MULTIBOOT_INFO_BOOT_LOADER_NAME; | |
208 | mbi->boot_loader_name = ptrdest; | |
209 | ptrorig += ALIGN_UP (sizeof(PACKAGE_STRING), 4); | |
210 | ptrdest += ALIGN_UP (sizeof(PACKAGE_STRING), 4); | |
211 | ||
212 | if (modcnt) | |
213 | { | |
214 | mbi->flags |= MULTIBOOT_INFO_MODS; | |
215 | mbi->mods_addr = ptrdest; | |
216 | mbi->mods_count = modcnt; | |
217 | modlist = (struct multiboot_mod_list *) ptrorig; | |
218 | ptrorig += modcnt * sizeof (struct multiboot_mod_list); | |
219 | ptrdest += modcnt * sizeof (struct multiboot_mod_list); | |
220 | ||
221 | for (i = 0, cur = modules; i < modcnt; i++, cur = cur->next) | |
222 | { | |
223 | modlist[i].mod_start = cur->start; | |
224 | modlist[i].mod_end = modlist[i].mod_start + cur->size; | |
225 | modlist[i].cmdline = ptrdest; | |
226 | grub_memcpy (ptrorig, cur->cmdline, cur->cmdline_size); | |
227 | ptrorig += ALIGN_UP (cur->cmdline_size, 4); | |
228 | ptrdest += ALIGN_UP (cur->cmdline_size, 4); | |
229 | } | |
230 | } | |
231 | else | |
232 | { | |
233 | mbi->mods_addr = 0; | |
234 | mbi->mods_count = 0; | |
235 | } | |
236 | ||
237 | mmap_size = grub_get_multiboot_mmap_count () | |
238 | * sizeof (struct multiboot_mmap_entry); | |
239 | grub_fill_multiboot_mmap ((struct multiboot_mmap_entry *) ptrorig); | |
240 | mbi->mmap_length = mmap_size; | |
241 | mbi->mmap_addr = ptrdest; | |
242 | mbi->flags |= MULTIBOOT_INFO_MEM_MAP; | |
243 | ptrorig += mmap_size; | |
244 | ptrdest += mmap_size; | |
245 | ||
246 | /* Convert from bytes to kilobytes. */ | |
247 | mbi->mem_lower = grub_mmap_get_lower () / 1024; | |
248 | mbi->mem_upper = grub_mmap_get_upper () / 1024; | |
249 | mbi->flags |= MULTIBOOT_INFO_MEMORY; | |
250 | ||
251 | if (bootdev_set) | |
252 | { | |
253 | mbi->boot_device = bootdev; | |
254 | mbi->flags |= MULTIBOOT_INFO_BOOTDEV; | |
255 | } | |
256 | ||
257 | err = retrieve_video_parameters (mbi, ptrorig, ptrdest); | |
258 | if (err) | |
259 | { | |
260 | grub_print_error (); | |
261 | grub_errno = GRUB_ERR_NONE; | |
262 | } | |
263 | #if GRUB_MACHINE_HAS_VBE | |
264 | ptrorig += sizeof (struct grub_vbe_info_block); | |
265 | ptrdest += sizeof (struct grub_vbe_info_block); | |
266 | ptrorig += sizeof (struct grub_vbe_mode_info_block); | |
267 | ptrdest += sizeof (struct grub_vbe_mode_info_block); | |
268 | #endif | |
269 | ||
270 | return GRUB_ERR_NONE; | |
271 | } | |
272 | ||
273 | void | |
274 | grub_multiboot_free_mbi (void) | |
275 | { | |
276 | struct module *cur, *next; | |
277 | ||
278 | cmdline_size = 0; | |
279 | total_modcmd = 0; | |
280 | modcnt = 0; | |
281 | grub_free (cmdline); | |
282 | cmdline = NULL; | |
283 | bootdev_set = 0; | |
284 | ||
285 | for (cur = modules; cur; cur = next) | |
286 | { | |
287 | next = cur->next; | |
288 | grub_free (cur->cmdline); | |
289 | grub_free (cur); | |
290 | } | |
291 | modules = NULL; | |
292 | modules_last = NULL; | |
293 | } | |
294 | ||
295 | grub_err_t | |
296 | grub_multiboot_init_mbi (int argc, char *argv[]) | |
297 | { | |
298 | grub_ssize_t len = 0; | |
299 | char *p; | |
300 | int i; | |
301 | ||
302 | grub_multiboot_free_mbi (); | |
303 | ||
304 | for (i = 0; i < argc; i++) | |
305 | len += grub_strlen (argv[i]) + 1; | |
306 | if (len == 0) | |
307 | len = 1; | |
308 | ||
309 | cmdline = p = grub_malloc (len); | |
310 | if (! cmdline) | |
311 | return grub_errno; | |
312 | cmdline_size = len; | |
313 | ||
314 | for (i = 0; i < argc; i++) | |
315 | { | |
316 | p = grub_stpcpy (p, argv[i]); | |
317 | *(p++) = ' '; | |
318 | } | |
319 | ||
320 | /* Remove the space after the last word. */ | |
321 | if (p != cmdline) | |
322 | p--; | |
323 | *p = '\0'; | |
324 | ||
325 | return GRUB_ERR_NONE; | |
326 | } | |
327 | ||
328 | grub_err_t | |
329 | grub_multiboot_add_module (grub_addr_t start, grub_size_t size, | |
330 | int argc, char *argv[]) | |
331 | { | |
332 | struct module *newmod; | |
333 | char *p; | |
334 | grub_ssize_t len = 0; | |
335 | int i; | |
336 | ||
337 | newmod = grub_malloc (sizeof (*newmod)); | |
338 | if (!newmod) | |
339 | return grub_errno; | |
340 | newmod->start = start; | |
341 | newmod->size = size; | |
342 | ||
343 | for (i = 0; i < argc; i++) | |
344 | len += grub_strlen (argv[i]) + 1; | |
345 | ||
346 | if (len == 0) | |
347 | len = 1; | |
348 | ||
349 | newmod->cmdline = p = grub_malloc (len); | |
350 | if (! newmod->cmdline) | |
351 | { | |
352 | grub_free (newmod); | |
353 | return grub_errno; | |
354 | } | |
355 | newmod->cmdline_size = len; | |
356 | total_modcmd += ALIGN_UP (len, 4); | |
357 | ||
358 | for (i = 0; i < argc; i++) | |
359 | { | |
360 | p = grub_stpcpy (p, argv[i]); | |
361 | *(p++) = ' '; | |
362 | } | |
363 | ||
364 | /* Remove the space after the last word. */ | |
365 | if (p != newmod->cmdline) | |
366 | p--; | |
367 | *p = '\0'; | |
368 | ||
369 | if (modules_last) | |
370 | modules_last->next = newmod; | |
371 | else | |
372 | { | |
373 | modules = newmod; | |
374 | modules_last->next = NULL; | |
375 | } | |
376 | modules_last = newmod; | |
377 | ||
378 | modcnt++; | |
379 | ||
380 | return GRUB_ERR_NONE; | |
381 | } | |
382 | ||
383 | void | |
384 | grub_multiboot_set_bootdev (void) | |
385 | { | |
386 | char *p; | |
387 | grub_uint32_t biosdev, slice = ~0, part = ~0; | |
388 | grub_device_t dev; | |
389 | ||
390 | #ifdef GRUB_MACHINE_PCBIOS | |
391 | biosdev = grub_get_root_biosnumber (); | |
392 | #else | |
393 | biosdev = 0xffffffff; | |
394 | #endif | |
395 | ||
396 | if (biosdev == 0xffffffff) | |
397 | return; | |
398 | ||
399 | dev = grub_device_open (0); | |
400 | if (dev && dev->disk && dev->disk->partition) | |
401 | { | |
402 | char *p0; | |
403 | p = p0 = dev->disk->partition->partmap->get_name (dev->disk->partition); | |
404 | if (p) | |
405 | { | |
406 | if ((p[0] >= '0') && (p[0] <= '9')) | |
407 | { | |
408 | slice = grub_strtoul (p, &p, 0) - 1; | |
409 | ||
410 | if ((p) && (p[0] == ',')) | |
411 | p++; | |
412 | } | |
413 | ||
414 | if ((p[0] >= 'a') && (p[0] <= 'z')) | |
415 | part = p[0] - 'a'; | |
416 | } | |
417 | grub_free (p0); | |
418 | } | |
419 | if (dev) | |
420 | grub_device_close (dev); | |
421 | ||
422 | bootdev = ((biosdev & 0xff) << 24) | ((slice & 0xff) << 16) | |
423 | | ((part & 0xff) << 8) | 0xff; | |
424 | bootdev_set = 1; | |
425 | } |