📄 stage2.c
字号:
comment = 0; } else if (! pos) { if (c == '#') comment = 1; else if ((c != ' ') && (c != '\n')) cmdline[pos++] = c; } else { if (c == '\n') break; if (pos < max_len) cmdline[pos++] = c; } } cmdline[pos] = 0; return pos;}//void//reset (void);static int config_len, menu_len, num_entries;static char *config_entries, *menu_entries, *cur_entry;static voidreset (void){ count_lines = -1; config_len = 0; menu_len = 0; num_entries = 0; config_entries = (char *) init_free_mem_start;//mbi.drives_addr + mbi.drives_length; cur_entry = config_entries; menu_entries = (char *) MENU_BUF; init_config ();} extern int commandline_func (char *arg1, int flags);extern int errnum_func (char *arg1, int flags);/* This is the starting function in C. */voidcmain (void){ char *kill_buf = (char *) KILL_BUF;#ifdef GRUB_UTIL /* Initialize the environment for restarting Stage 2. */ grub_setjmp (restart_env);#endif /* GRUB_UTIL */ /* Initialize the kill buffer. */ *kill_buf = 0;#ifndef GRUB_UTIL debug = debug_boot + 1; pxe_detect();#endif /* ! GRUB_UTIL */ /* Never return. */ for (;;) { extern int use_config_file; int is_opened, is_preset; int i; reset (); /* Here load the configuration file. */ //#ifdef GRUB_UTIL if (use_config_file)//#endif /* GRUB_UTIL */ { char *default_file = (char *) DEFAULT_FILE_BUF; /* Get a saved default entry if possible. */ saved_entryno = 0; if (*config_file) { *default_file = 0; /* initialise default_file */ grub_strncat (default_file, config_file, DEFAULT_FILE_BUFLEN); for (i = grub_strlen (default_file); i >= 0; i--) if (default_file[i] == '/') { //i++; break; } default_file[++i] = 0; grub_strncat (default_file + i, "default", DEFAULT_FILE_BUFLEN - i); if (debug > 1) grub_printf("Open %s ... ", default_file);// i=grub_open (default_file);// printf("default_file ok=%s\n", default_file);// for (;;); DEBUG_SLEEP if (grub_open (default_file)) { char buf[10]; /* This is good enough. */ char *p = buf; int len; if (debug > 1) grub_printf("Read file: ", default_file); len = grub_read (buf, sizeof (buf)); if (debug > 1) grub_printf("len=%d\n", len); if (len > 0) { buf[sizeof (buf) - 1] = 0; safe_parse_maxint (&p, &saved_entryno); } grub_close (); } else if (debug > 1) grub_printf("failure.\n", default_file); DEBUG_SLEEP } errnum = ERR_NONE; #ifdef GRUB_UTIL do#endif /* GRUB_UTIL */ { /* STATE 0: Before any title command. STATE 1: In a title command. STATE >1: In a entry after a title command. */ int state = 0, prev_config_len = 0, prev_menu_len = 0; char *cmdline; is_preset = is_opened = 0; /* Try command-line menu first if it is specified. */ if (preset_menu == (char *)0x0800 && ! *config_file) { is_opened = is_preset = open_preset_menu (); } if (! is_opened) { /* Try config_file */ if (*config_file) is_opened = grub_open (config_file); } errnum = ERR_NONE; if (! is_opened) { /* Try the preset menu. This will succeed at most once, * because close_preset_menu disables the preset menu. */ is_opened = is_preset = open_preset_menu (); } if (! is_opened) break; /* This is necessary, because the menu must be overrided. */ reset (); cmdline = (char *) CMDLINE_BUF; while (get_line_from_config (cmdline, NEW_HEAPSIZE, ! is_preset)) { struct builtin *builtin; /* Get the pointer to the builtin structure. */ builtin = find_command (cmdline); errnum = 0; if (! builtin) /* Unknown command. Just skip now. */ continue; if (builtin->flags & BUILTIN_TITLE) { char *ptr; /* the command "title" is specially treated. */ if (state > 1) { /* The next title is found. */ num_entries++; config_entries[config_len++] = 0; prev_menu_len = menu_len; prev_config_len = config_len; } else { /* The first title is found. */ menu_len = prev_menu_len; config_len = prev_config_len; } /* Reset the state. */ state = 1; /* Copy title into menu area. */ ptr = skip_to (1, cmdline); while ((menu_entries[menu_len++] = *(ptr++)) != 0) ; } else if (! state) { /* Run a command found is possible. */ if (builtin->flags & BUILTIN_MENU) {#if 0 extern int commandline_func (char *arg, int flags); char *arg = (builtin->func) == commandline_func ? cmdline : skip_to (1, cmdline); (builtin->func) (arg, BUILTIN_MENU); errnum = 0;#endif char *ptr = cmdline; /* Copy menu-specific commands to config area. */ while ((config_entries[config_len++] = *ptr++) != 0); prev_config_len = config_len; } else /* Ignored. */ continue; } else { char *ptr = cmdline; /* state == 1 means it is immediately after a TITLE, and * num_entries == 0 means the TITLE is the first one. */ if (num_entries == 0 && state == 1) { /* Finish the menu-specific commands. */ config_entries[config_len++] = 0; } state++; /* Copy config file data to config area. */ while ((config_entries[config_len++] = *ptr++) != 0) ; } } /* while */ /* file must be closed here, because the menu-specific commands * below may also use the GRUB_OPEN command. */ if (is_preset) close_preset_menu (); else grub_close (); if (state > 1) { /* Finish the last entry. */ num_entries++; config_entries[config_len++] = 0; } else// if (state) { menu_len = prev_menu_len; config_len = prev_config_len; } //else ///* Finish the menu-specific commands. */ //config_entries[config_len++] = 0; menu_entries[menu_len++] = 0; config_entries[config_len++] = 0; grub_memmove (config_entries + config_len, menu_entries, menu_len); menu_entries = config_entries + config_len; /* Run menu-specific commands before any other menu entry commands. */ { char *old_entry; char *heap = menu_entries + menu_len;#ifndef GRUB_UTIL int old_debug = 1;#endif /* ! GRUB_UTIL */ /* Initialize the data. */ extern void init_cmdline (void); init_cmdline (); while (1) { struct builtin *builtin; char *arg; grub_error_t errnum_old; errnum_old = errnum; errnum = 0; /* Copy the first string in CUR_ENTRY to HEAP. */ old_entry = cur_entry; while (*cur_entry++) ; grub_memmove (heap, old_entry, (int) cur_entry - (int) old_entry); if (! *heap) { /* If there is no more command in SCRIPT... */ /* If no kernel is loaded, just exit successfully. */ if (kernel_type == KERNEL_TYPE_NONE) break; /* Otherwise, the command boot is run implicitly. */ grub_memmove (heap, "boot", 5); } /* Find a builtin. */ builtin = find_command (heap); if (! builtin) { grub_printf ("%s\n", old_entry); continue; }/* now we do not echo menu-specific commands */// if (! (builtin->flags & BUILTIN_NO_ECHO))// grub_printf ("%s\n", old_entry); /* If BUILTIN cannot be run in the menu, skip it. */ if (! (builtin->flags & BUILTIN_MENU)) { continue; }#if 0 /* Invalidate the cache, because the user may exchange removable disks. */ buf_drive = -1;#endif if ((builtin->func) == errnum_func) errnum = errnum_old; /* find && and || */ for (arg = skip_to (0, heap); *arg != 0; arg = skip_to (0, arg)) { struct builtin *builtin1; int ret; char *arg1; arg1 = arg; if (*arg == '&' && arg[1] == '&' && (arg[2] == ' ' || arg[2] == '\t')) { /* handle the AND operator */ arg = skip_to (0, arg); builtin1 = find_command (arg); if (! builtin1 || ! (builtin1->flags & BUILTIN_MENU)) { errnum = ERR_UNRECOGNIZED; goto next; } *arg1 = 0; ret = (builtin->func) (skip_to (1, heap), BUILTIN_MENU); *arg1 = '&'; if (ret) { arg = skip_to (1, arg); if ((builtin1->func) != errnum_func) errnum = 0; (builtin1->func) (arg, BUILTIN_MENU); } else errnum = 0; goto next; } else if (*arg == '|' && arg[1] == '|' && (arg[2] == ' ' || arg[2] == '\t')) { /* handle the OR operator */ arg = skip_to (0, arg); builtin1 = find_command (arg); if (! builtin1 || ! (builtin1->flags & BUILTIN_MENU)) { errnum = ERR_UNRECOGNIZED; goto next; } *arg1 = 0; ret = (builtin->func) (skip_to (1, heap), BUILTIN_MENU); *arg1 = '|'; if (! ret) { arg = skip_to (1, arg); if ((builtin1->func) != errnum_func) errnum = 0; (builtin1->func) (arg, BUILTIN_MENU); } else errnum = 0; goto next; } } /* Run BUILTIN->FUNC. */ arg = (builtin->func) == commandline_func ? heap : skip_to (1, heap); (builtin->func) (arg, BUILTIN_MENU); /* if the INSERT key was pressed at startup, debug is not allowed to be turned off. */#ifndef GRUB_UTIL if (debug_boot) if ((unsigned int)debug < 2) /* debug == 0 or 1 */ { old_debug = debug; /* save the new debug in old_debug */ debug = 2; }#endif /* ! GRUB_UTIL */next: DEBUG_SLEEP if (! *old_entry) break; } //config_entries = cur_entry; /* config file data begins here */#ifndef GRUB_UTIL if (debug_boot) { debug = old_debug; grub_printf ("\n\nEnd of menu init commands. Press any key to enter command-line or run menu...", old_entry); }#endif /* ! GRUB_UTIL */ DEBUG_SLEEP} errnum = 0; /* Make sure that all fallback entries are valid. */ if (fallback_entryno >= 0) { for (i = 0; i < MAX_FALLBACK_ENTRIES; i++) { if (fallback_entries[i] < 0) break; if (fallback_entries[i] >= num_entries) { grub_memmove (fallback_entries + i, fallback_entries + i + 1, ((MAX_FALLBACK_ENTRIES - i - 1) * sizeof (int))); i--; } } if (fallback_entries[0] < 0) fallback_entryno = -1; } /* Check if the default entry is present. Otherwise reset it to fallback if fallback is valid, or to DEFAULT_ENTRY if not. */ if (default_entry >= num_entries) { if (fallback_entryno >= 0) { default_entry = fallback_entries[0]; fallback_entryno++; if (fallback_entryno >= MAX_FALLBACK_ENTRIES || fallback_entries[fallback_entryno] < 0) fallback_entryno = -1; } else default_entry = 0; } }#ifdef GRUB_UTIL while (is_preset);#endif /* GRUB_UTIL */ } /* go ahead and make sure the terminal is setup */ if (current_term->startup) (*current_term->startup)(); if (! num_entries) { /* If no acceptable config file, goto command-line, starting heap from where the config entries would have been stored if there were any. */ enter_cmdline (config_entries, 1); } else { /* Run menu interface. */ run_menu (menu_entries, cur_entry, num_entries, menu_entries + menu_len, default_entry); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -