📄 nfl.c
字号:
#include "stddef.h"#include "string.h"#include "linux-asm-io.h"#include "string.h"#include "etherboot.h"#include "startmenu.h"#include "elf_boot.h"#include "nfl.h"/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2, or (at * your option) any later version. *//*This is an example program which shows how the extension routinefeature in Etherboot 5.0 works.This program presents a list of valid boot images from a data segmentthat is loaded into another area of memory, and prompts the user for anumber, and modifies the bootp record to the filename to be loaded. Youcan make the menu program as elaborate as you like, the sky is thelimit.Ideally, there should be a menu generation program that takes ahigh-level description of menus and valid inputs and creates a datafile to be loaded to the data area. The menu program should agree withthe menu generator on the layout of the data area.This program is linked to run at 0x60000, and expects to find configdata at 0x70000. This means the code can be up to 64kB long.When the program starts it receives 3 parameters from Etherboot:Pointer to ebinfo structurePointer to image header structure (either a tagged or ELF image header)Pointer to bootp/DHCP reply obtained by Etherboot from bootpd or DHCPDEtherboot expects this program to return an int. The values have thesemeanings:<0 Do not use0 Same as 1, for implementation reasons1 Redo tftp with possibly modified bootp record2 Redo bootp and tftp255 Exit EtherbootObserve that this program causes Etherboot to load a different programnext by modifying the contents of the filename field in the bootp recordand then returning 1. It can also send parameters to the next program bymodifying tag 129 in the bootp record. This is how the menu systemworks.The data segment that this particular program expects is of the form choice 1\nchoice 2\nchoice 3\n\0where the \n are newlines and the \0 the teminating zero byte. Thereforeyou can create this file with a Unix text editor (but see nextparagraph). choice 1, etc are the pathnames or filenames of the nextfile to load by TFTP. If the string starts with / then it's assumed tobe a pathname and the whole of the bootp filename area is replaced byit, otherwise just the filename portion of the pathname, i.e. the samedirectory as the menu file is assumed.This program also illustrates the use of a timeout to select a defaultitem by using currticks() to obtain the value of the BIOS clock andconsole_ischar to determine if a character has been typed at thekeyboard.Commentary: This program is just to illustrate a very simple menusystem. There are known bugs:mknbi-menu/mkelf-menu does not add the ending NUL byte, but this ispresent due to the NUL fill to the next block boundary. If the size ofthe data goes exactly up to a block boundary, then it is possible therewill be a spurious final item that goes up the next NUL byte in memory.Another bug is that there is no overflow checking when writing intobootp->bp_file.Yet another bug is that there is no facility to correct input entry onlines. getline() should be smarter.*//*Memory layout assumed by mknbi and this program0x60000-0x6FFFF 64 kB Menu program0x70000-0x7FFFF 64 kB Menu data (initial)*/#define NFL_VERSION "0.56"#define TIMEOUT 10 /* seconds */#define MENU_DATA ((char *)0x70000)#define MENU_COLS 75#define MENU_ROWS 14#define MAX_ENTRIES 100static int dbg_count1=0;static char *menu_entries[MAX_ENTRIES];static unsigned short num_entries;static unsigned short cur_entry;static unsigned short top_entry;static int timeout = 0;/* Color settings. */static int color_fg = 7;static int color_bg = 4;static int number_entries = 0;/* Default Menu Size */static unsigned short menu_ulx = 1;static unsigned short menu_uly = 1;static unsigned short menu_rows = 0x0800;static unsigned short menu_cols = 0x0800;/* Terminal types. */static int terminal = TERMINAL_CONSOLE;/* Defined in ANSI.C */extern unsigned short rows, columns, attr;extern void ansi_putc(unsigned int);extern void ansi_reset(void);extern void enable_cursor(int);extern void printf(const char *, ...);extern void sprintf(char *, const char *, ...);extern char *strcat(char *, const char *);extern char *strncat(char *, const char *, unsigned int);/*--------------------------------------------------------------------------*/void putchar(int c){ if (c == '\n') ansi_putc('\r'); ansi_putc(c);}/*--------------------------------------------------------------------------*/int getchar(void){ int c; c = console_getc(); if (c == '\r') c = '\n'; return (c);}/* Wait for a keypress and return its code.--------------------------------------------------------------------------*/int getkey (void){ int c = -1; if ((terminal & TERMINAL_CONSOLE)#ifdef SUPPORT_HERCULES || (terminal & TERMINAL_HERCULES)#endif /* SUPPORT_HERCULES */ ) c = console_getkey ();#ifdef SUPPORT_SERIAL else if (terminal & TERMINAL_SERIAL) c = serial_getkey ();#endif /* SUPPORT_SERIAL */ return c;}/* Check if a key code is available.--------------------------------------------------------------------------*/int checkkey (void){ int c = -1; if ((terminal & TERMINAL_CONSOLE)#ifdef SUPPORT_HERCULES || (terminal & TERMINAL_HERCULES)#endif /* SUPPORT_HERCULES */ ) c = console_checkkey (); #ifdef SUPPORT_SERIAL if (terminal & TERMINAL_SERIAL) c = serial_checkkey ();#endif /* SUPPORT_SERIAL */ return c;}/* Translate a special key to a common ascii code.--------------------------------------------------------------------------*/int translate_keycode (int c){#if 0 //def SUPPORT_SERIAL if (terminal & TERMINAL_SERIAL) { /* In a serial terminal, things are complicated, because several key codes start from the character ESC, while we want to accept ESC itself. */ if (c == '\e') { int start; /* Get current time. */ start = currticks (); while (checkkey () == -1) { /* Wait for a next character, at least for 0.1 sec (18.2 ticks/sec). */ int now; now = currticks (); if (now - start >= 2) return c; } c = getkey (); if (c == '[') { int c1, c2; /* To filter illegal states. */ c = 0; c1 = getkey (); switch (c1) { case 'A': /* KEY_UP */ c = 16; break; case 'B': /* KEY_DOWN */ c = 14; break; case 'C': /* KEY_RIGHT */ c = 6; break; case 'D': /* KEY_LEFT */ c = 2; break; case 'F': /* End */ c = 5; break; case 'H': /* Home */ c = 1; break; case '1': c2 = getkey (); if (c2 == '~') { /* One of control keys (pos1,....). */ c = 1; } break; case '3': c2 = getkey (); if (c2 == '~') { /* One of control keys (del,....). */ c = 4; } break; case '4': /* Del */ c = 4; break; } } } } else# endif /* SUPPORT_SERIAL */ { switch (c) { case KEY_LEFT: c = 2; break; case KEY_RIGHT: c = 6; break; case KEY_UP: c = 16; break; case KEY_DOWN: c = 14; break; case KEY_HOME: c = 1; break; case KEY_END: c = 5; break; case KEY_DC: c = 4; break; case KEY_BACKSPACE: c = 8; break; } } return ASCII_CHAR (c);}/* Get a line, ignore characters after array limit reached. Echo the character if the flag says so.--------------------------------------------------------------------------*/int getline(char *line, int length, int echo){ int c; char *p = line; while ((c = getchar()) != '\n') { if (p < &line[length-1]) { /* within array? */ if (echo) ansi_putc(c); *p++ = c; } } *p++ = '\0'; if (echo) ansi_putc('\n'); return (line - p);}/* Build menu entries array starting at index one for clarity...--------------------------------------------------------------------------*/int scan_entries(void){ int i = 0, first_entry_char = 1; char *p = MENU_DATA; if (p == '\0') return( 0 ); do { /* Skip leading white-space */ while ((*p == '\b') || (*p == '\t')) p++; menu_entries[i+1] = p; while ((*p != '\0') && (*p != '\n')) { /* Point to next character */ p++; /* At least one non-blank character, index to next entry */ if (first_entry_char) { first_entry_char = 0; i++; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -