]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/kern/main.c
Update Hindi Translation (#1941)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / kern / main.c
1 /* main.c - the kernel main routine */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2008,2009 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/kernel.h>
21 #include <grub/misc.h>
22 #include <grub/symbol.h>
23 #include <grub/dl.h>
24 #include <grub/term.h>
25 #include <grub/file.h>
26 #include <grub/device.h>
27 #include <grub/env.h>
28 #include <grub/mm.h>
29 #include <grub/command.h>
30 #include <grub/reader.h>
31 #include <grub/parser.h>
32
33 #ifdef GRUB_MACHINE_PCBIOS
34 #include <grub/machine/memory.h>
35 #endif
36
37 grub_addr_t
38 grub_modules_get_end (void)
39 {
40 struct grub_module_info *modinfo;
41
42 modinfo = (struct grub_module_info *) grub_modbase;
43
44 /* Check if there are any modules. */
45 if ((modinfo == 0) || modinfo->magic != GRUB_MODULE_MAGIC)
46 return grub_modbase;
47
48 return grub_modbase + modinfo->size;
49 }
50
51 /* Load all modules in core. */
52 static void
53 grub_load_modules (void)
54 {
55 struct grub_module_header *header;
56 FOR_MODULES (header)
57 {
58 /* Not an ELF module, skip. */
59 if (header->type != OBJ_TYPE_ELF)
60 continue;
61
62 if (! grub_dl_load_core ((char *) header + sizeof (struct grub_module_header),
63 (header->size - sizeof (struct grub_module_header))))
64 grub_fatal ("%s", grub_errmsg);
65
66 if (grub_errno)
67 grub_print_error ();
68 }
69 }
70
71 static char *load_config;
72
73 static void
74 grub_load_config (void)
75 {
76 struct grub_module_header *header;
77 FOR_MODULES (header)
78 {
79 /* Not an embedded config, skip. */
80 if (header->type != OBJ_TYPE_CONFIG)
81 continue;
82
83 load_config = grub_malloc (header->size - sizeof (struct grub_module_header) + 1);
84 if (!load_config)
85 {
86 grub_print_error ();
87 break;
88 }
89 grub_memcpy (load_config, (char *) header +
90 sizeof (struct grub_module_header),
91 header->size - sizeof (struct grub_module_header));
92 load_config[header->size - sizeof (struct grub_module_header)] = 0;
93 break;
94 }
95 }
96
97 /* Write hook for the environment variables of root. Remove surrounding
98 parentheses, if any. */
99 static char *
100 grub_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
101 const char *val)
102 {
103 /* XXX Is it better to check the existence of the device? */
104 grub_size_t len = grub_strlen (val);
105
106 if (val[0] == '(' && val[len - 1] == ')')
107 return grub_strndup (val + 1, len - 2);
108
109 return grub_strdup (val);
110 }
111
112 static int g_ventoy_hook_root = 0;
113 void ventoy_env_hook_root(int hook)
114 {
115 g_ventoy_hook_root = hook;
116 }
117
118 static char *
119 ventoy_env_write_root (struct grub_env_var *var __attribute__ ((unused)),
120 const char *val)
121 {
122 const char *pos = val;
123 char buf[256];
124
125 if (g_ventoy_hook_root == 0)
126 {
127 return grub_env_write_root(var, val);
128 }
129
130 if (pos[0] == '(')
131 {
132 pos++;
133 }
134
135 if (grub_strncmp(pos, "vtimghd", 7) == 0)
136 {
137 return grub_env_write_root(var, val);
138 }
139
140 pos = grub_strchr(val, ',');
141 if (!pos)
142 {
143 return grub_env_write_root(var, val);
144 }
145
146 if (val[0] == '(')
147 {
148 grub_snprintf(buf, sizeof(buf), "(vtimghd%s", pos);
149 }
150 else
151 {
152 grub_snprintf(buf, sizeof(buf), "vtimghd%s", pos);
153 }
154
155 return grub_env_write_root(var, buf);
156 }
157
158 static void
159 grub_set_prefix_and_root (void)
160 {
161 char *device = NULL;
162 char *path = NULL;
163 char *fwdevice = NULL;
164 char *fwpath = NULL;
165 char *prefix = NULL;
166 struct grub_module_header *header;
167
168 FOR_MODULES (header)
169 if (header->type == OBJ_TYPE_PREFIX)
170 prefix = (char *) header + sizeof (struct grub_module_header);
171
172 grub_register_variable_hook ("root", 0, ventoy_env_write_root);
173
174 grub_machine_get_bootlocation (&fwdevice, &fwpath);
175
176 if (fwdevice)
177 {
178 char *cmdpath;
179
180 cmdpath = grub_xasprintf ("(%s)%s", fwdevice, fwpath ? : "");
181 if (cmdpath)
182 {
183 grub_env_set ("cmdpath", cmdpath);
184 grub_env_export ("cmdpath");
185 grub_free (cmdpath);
186 }
187 }
188
189 if (prefix)
190 {
191 char *pptr = NULL;
192 if (prefix[0] == '(')
193 {
194 pptr = grub_strrchr (prefix, ')');
195 if (pptr)
196 {
197 device = grub_strndup (prefix + 1, pptr - prefix - 1);
198 pptr++;
199 }
200 }
201 if (!pptr)
202 pptr = prefix;
203 if (pptr[0])
204 path = grub_strdup (pptr);
205 }
206
207 if (!device && fwdevice)
208 device = fwdevice;
209 else if (fwdevice && (device[0] == ',' || !device[0]))
210 {
211 /* We have a partition, but still need to fill in the drive. */
212 char *comma, *new_device;
213
214 for (comma = fwdevice; *comma; )
215 {
216 if (comma[0] == '\\' && comma[1] == ',')
217 {
218 comma += 2;
219 continue;
220 }
221 if (*comma == ',')
222 break;
223 comma++;
224 }
225 if (*comma)
226 {
227 char *drive = grub_strndup (fwdevice, comma - fwdevice);
228 new_device = grub_xasprintf ("%s%s", drive, device);
229 grub_free (drive);
230 }
231 else
232 new_device = grub_xasprintf ("%s%s", fwdevice, device);
233
234 grub_free (fwdevice);
235 grub_free (device);
236 device = new_device;
237 }
238 else
239 grub_free (fwdevice);
240 if (fwpath && !path)
241 {
242 grub_size_t len = grub_strlen (fwpath);
243 while (len > 1 && fwpath[len - 1] == '/')
244 fwpath[--len] = 0;
245 if (len >= sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1
246 && grub_memcmp (fwpath + len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1), GRUB_TARGET_CPU "-" GRUB_PLATFORM,
247 sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1) == 0)
248 fwpath[len - (sizeof (GRUB_TARGET_CPU "-" GRUB_PLATFORM) - 1)] = 0;
249 path = fwpath;
250 }
251 else
252 grub_free (fwpath);
253 if (device)
254 {
255 char *prefix_set;
256
257 prefix_set = grub_xasprintf ("(%s)%s", device, path ? : "");
258 if (prefix_set)
259 {
260 grub_env_set ("prefix", prefix_set);
261 grub_free (prefix_set);
262 }
263 grub_env_set ("root", device);
264 }
265
266 grub_free (device);
267 grub_free (path);
268 grub_print_error ();
269 }
270
271 /* Load the normal mode module and execute the normal mode if possible. */
272 static void
273 grub_load_normal_mode (void)
274 {
275 /* Load the module. */
276 grub_dl_load ("normal");
277
278 /* Print errors if any. */
279 grub_print_error ();
280 grub_errno = 0;
281
282 grub_command_execute ("normal", 0, 0);
283 }
284
285 static void
286 reclaim_module_space (void)
287 {
288 grub_addr_t modstart, modend;
289
290 if (!grub_modbase)
291 return;
292
293 #ifdef GRUB_MACHINE_PCBIOS
294 modstart = GRUB_MEMORY_MACHINE_DECOMPRESSION_ADDR;
295 #else
296 modstart = grub_modbase;
297 #endif
298 modend = grub_modules_get_end ();
299 grub_modbase = 0;
300
301 #if GRUB_KERNEL_PRELOAD_SPACE_REUSABLE
302 grub_mm_init_region ((void *) modstart, modend - modstart);
303 #else
304 (void) modstart;
305 (void) modend;
306 #endif
307 }
308
309 #ifndef GRUB_MACHINE_EFI
310 static int ventoy_legacy_limit_workaround(void)
311 {
312 grub_file_t file;
313 char *pos, *root;
314 char buf[128];
315
316 root = grub_strdup(grub_env_get("root"));
317 if (!root)
318 {
319 return 1;
320 }
321
322 pos = grub_strchr(root, ',');
323 if (pos) *pos = 0;
324
325 grub_snprintf(buf, sizeof(buf), "(%s,1)/ventoy/ventoy.disk.img.xz", root);
326 file = grub_file_open(buf, GRUB_FILE_TYPE_NONE);
327 if (file)
328 {
329 pos = grub_malloc(file->size);
330 if (pos)
331 {
332 grub_file_read(file, pos, file->size);
333 grub_snprintf(buf, sizeof(buf), "loopback ventoydisk mem:0x%lx:size:%lu",
334 (unsigned long)pos, (unsigned long)file->size);
335
336 grub_parser_execute(buf);
337 grub_env_set("prefix", "(ventoydisk)/grub");
338 }
339
340 grub_file_close(file);
341 }
342
343 grub_free(root);
344 return 0;
345 }
346 #endif
347
348 /* The main routine. */
349 void __attribute__ ((noreturn))
350 grub_main (void)
351 {
352 /* First of all, initialize the machine. */
353 grub_machine_init ();
354
355 grub_boot_time ("After machine init.");
356
357 /* Hello. */
358 grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
359 //grub_printf ("Welcome to GRUB!\n\n");
360 grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
361
362 grub_load_config ();
363
364 grub_boot_time ("Before loading embedded modules.");
365
366 /* Load pre-loaded modules and free the space. */
367 grub_register_exported_symbols ();
368 #ifdef GRUB_LINKER_HAVE_INIT
369 grub_arch_dl_init_linker ();
370 #endif
371 grub_load_modules ();
372
373 grub_boot_time ("After loading embedded modules.");
374
375 /* It is better to set the root device as soon as possible,
376 for convenience. */
377 grub_set_prefix_and_root ();
378 grub_env_export ("root");
379 grub_env_export ("prefix");
380
381 #ifndef GRUB_MACHINE_EFI
382 if (0 == ventoy_check_file_exist("%s/grub.cfg", grub_env_get("prefix"))) {
383 ventoy_legacy_limit_workaround();
384 }
385 #endif
386
387 /* Reclaim space used for modules. */
388 reclaim_module_space ();
389
390 grub_boot_time ("After reclaiming module space.");
391
392 grub_register_core_commands ();
393
394 grub_boot_time ("Before execution of embedded config.");
395
396 if (load_config)
397 grub_parser_execute (load_config);
398
399 grub_boot_time ("After execution of embedded config. Attempt to go to normal mode");
400
401 grub_load_normal_mode ();
402 grub_rescue_run ();
403 }