boot.c revision 1.5.2.2 1 1.5.2.2 pgoyette /* $NetBSD: boot.c,v 1.5.2.2 2018/09/06 06:56:47 pgoyette Exp $ */
2 1.5.2.2 pgoyette
3 1.5.2.2 pgoyette /*-
4 1.5.2.2 pgoyette * Copyright (c) 2016 Kimihiro Nonaka <nonaka (at) netbsd.org>
5 1.5.2.2 pgoyette * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
6 1.5.2.2 pgoyette * All rights reserved.
7 1.5.2.2 pgoyette *
8 1.5.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
9 1.5.2.2 pgoyette * modification, are permitted provided that the following conditions
10 1.5.2.2 pgoyette * are met:
11 1.5.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
12 1.5.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
13 1.5.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
14 1.5.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
15 1.5.2.2 pgoyette * documentation and/or other materials provided with the distribution.
16 1.5.2.2 pgoyette *
17 1.5.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 1.5.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.5.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.5.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 1.5.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.5.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.5.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.5.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.5.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.5.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.5.2.2 pgoyette * SUCH DAMAGE.
28 1.5.2.2 pgoyette */
29 1.5.2.2 pgoyette
30 1.5.2.2 pgoyette #include "efiboot.h"
31 1.5.2.2 pgoyette #include "efiblock.h"
32 1.5.2.2 pgoyette #include "efifdt.h"
33 1.5.2.2 pgoyette
34 1.5.2.2 pgoyette #include <sys/bootblock.h>
35 1.5.2.2 pgoyette #include <sys/boot_flag.h>
36 1.5.2.2 pgoyette #include <machine/limits.h>
37 1.5.2.2 pgoyette
38 1.5.2.2 pgoyette #include <loadfile.h>
39 1.5.2.2 pgoyette
40 1.5.2.2 pgoyette extern const char bootprog_name[], bootprog_rev[], bootprog_kernrev[];
41 1.5.2.2 pgoyette
42 1.5.2.2 pgoyette extern char twiddle_toggle;
43 1.5.2.2 pgoyette
44 1.5.2.2 pgoyette static const char * const names[][2] = {
45 1.5.2.2 pgoyette { "netbsd", "netbsd.gz" },
46 1.5.2.2 pgoyette { "onetbsd", "onetbsd.gz" },
47 1.5.2.2 pgoyette { "netbsd.old", "netbsd.old.gz" },
48 1.5.2.2 pgoyette };
49 1.5.2.2 pgoyette
50 1.5.2.2 pgoyette #define NUMNAMES __arraycount(names)
51 1.5.2.2 pgoyette #define DEFFILENAME names[0][0]
52 1.5.2.2 pgoyette
53 1.5.2.2 pgoyette #define DEFTIMEOUT 5
54 1.5.2.2 pgoyette
55 1.5.2.2 pgoyette static char default_device[32];
56 1.5.2.2 pgoyette
57 1.5.2.2 pgoyette void command_boot(char *);
58 1.5.2.2 pgoyette void command_dev(char *);
59 1.5.2.2 pgoyette void command_ls(char *);
60 1.5.2.2 pgoyette void command_reset(char *);
61 1.5.2.2 pgoyette void command_version(char *);
62 1.5.2.2 pgoyette void command_quit(char *);
63 1.5.2.2 pgoyette
64 1.5.2.2 pgoyette const struct boot_command commands[] = {
65 1.5.2.2 pgoyette { "boot", command_boot, "boot [fsN:][filename] [args]\n (ex. \"fs0:\\netbsd.old -s\"" },
66 1.5.2.2 pgoyette { "dev", command_dev, "dev" },
67 1.5.2.2 pgoyette { "ls", command_ls, "ls [hdNn:/path]" },
68 1.5.2.2 pgoyette { "version", command_version, "version" },
69 1.5.2.2 pgoyette { "help", command_help, "help|?" },
70 1.5.2.2 pgoyette { "?", command_help, NULL },
71 1.5.2.2 pgoyette { "quit", command_quit, "quit" },
72 1.5.2.2 pgoyette { NULL, NULL },
73 1.5.2.2 pgoyette };
74 1.5.2.2 pgoyette
75 1.5.2.2 pgoyette void
76 1.5.2.2 pgoyette command_help(char *arg)
77 1.5.2.2 pgoyette {
78 1.5.2.2 pgoyette int n;
79 1.5.2.2 pgoyette
80 1.5.2.2 pgoyette printf("commands are:\n");
81 1.5.2.2 pgoyette for (n = 0; commands[n].c_name; n++) {
82 1.5.2.2 pgoyette if (commands[n].c_help)
83 1.5.2.2 pgoyette printf("%s\n", commands[n].c_help);
84 1.5.2.2 pgoyette }
85 1.5.2.2 pgoyette }
86 1.5.2.2 pgoyette
87 1.5.2.2 pgoyette void
88 1.5.2.2 pgoyette command_boot(char *arg)
89 1.5.2.2 pgoyette {
90 1.5.2.2 pgoyette char *fname = arg;
91 1.5.2.2 pgoyette char *bootargs = gettrailer(arg);
92 1.5.2.2 pgoyette
93 1.5.2.2 pgoyette exec_netbsd(*fname ? fname : DEFFILENAME, bootargs);
94 1.5.2.2 pgoyette }
95 1.5.2.2 pgoyette
96 1.5.2.2 pgoyette void
97 1.5.2.2 pgoyette command_dev(char *arg)
98 1.5.2.2 pgoyette {
99 1.5.2.2 pgoyette if (arg && *arg) {
100 1.5.2.2 pgoyette set_default_device(arg);
101 1.5.2.2 pgoyette } else {
102 1.5.2.2 pgoyette efi_block_show();
103 1.5.2.2 pgoyette efi_net_show();
104 1.5.2.2 pgoyette efi_pxe_show();
105 1.5.2.2 pgoyette }
106 1.5.2.2 pgoyette
107 1.5.2.2 pgoyette if (strlen(default_device) > 0) {
108 1.5.2.2 pgoyette printf("\n");
109 1.5.2.2 pgoyette printf("default: %s\n", default_device);
110 1.5.2.2 pgoyette }
111 1.5.2.2 pgoyette }
112 1.5.2.2 pgoyette
113 1.5.2.2 pgoyette void
114 1.5.2.2 pgoyette command_ls(char *arg)
115 1.5.2.2 pgoyette {
116 1.5.2.2 pgoyette ls(arg);
117 1.5.2.2 pgoyette }
118 1.5.2.2 pgoyette
119 1.5.2.2 pgoyette void
120 1.5.2.2 pgoyette command_version(char *arg)
121 1.5.2.2 pgoyette {
122 1.5.2.2 pgoyette char *ufirmware;
123 1.5.2.2 pgoyette int rv;
124 1.5.2.2 pgoyette
125 1.5.2.2 pgoyette printf("EFI version: %d.%02d\n",
126 1.5.2.2 pgoyette ST->Hdr.Revision >> 16, ST->Hdr.Revision & 0xffff);
127 1.5.2.2 pgoyette ufirmware = NULL;
128 1.5.2.2 pgoyette rv = ucs2_to_utf8(ST->FirmwareVendor, &ufirmware);
129 1.5.2.2 pgoyette if (rv == 0) {
130 1.5.2.2 pgoyette printf("EFI Firmware: %s (rev %d.%02d)\n", ufirmware,
131 1.5.2.2 pgoyette ST->FirmwareRevision >> 16,
132 1.5.2.2 pgoyette ST->FirmwareRevision & 0xffff);
133 1.5.2.2 pgoyette FreePool(ufirmware);
134 1.5.2.2 pgoyette }
135 1.5.2.2 pgoyette
136 1.5.2.2 pgoyette efi_fdt_show();
137 1.5.2.2 pgoyette }
138 1.5.2.2 pgoyette
139 1.5.2.2 pgoyette void
140 1.5.2.2 pgoyette command_quit(char *arg)
141 1.5.2.2 pgoyette {
142 1.5.2.2 pgoyette efi_exit();
143 1.5.2.2 pgoyette }
144 1.5.2.2 pgoyette
145 1.5.2.2 pgoyette int
146 1.5.2.2 pgoyette set_default_device(char *arg)
147 1.5.2.2 pgoyette {
148 1.5.2.2 pgoyette if (strlen(arg) + 1 > sizeof(default_device))
149 1.5.2.2 pgoyette return ERANGE;
150 1.5.2.2 pgoyette strcpy(default_device, arg);
151 1.5.2.2 pgoyette return 0;
152 1.5.2.2 pgoyette }
153 1.5.2.2 pgoyette
154 1.5.2.2 pgoyette char *
155 1.5.2.2 pgoyette get_default_device(void)
156 1.5.2.2 pgoyette {
157 1.5.2.2 pgoyette return default_device;
158 1.5.2.2 pgoyette }
159 1.5.2.2 pgoyette
160 1.5.2.2 pgoyette void
161 1.5.2.2 pgoyette print_banner(void)
162 1.5.2.2 pgoyette {
163 1.5.2.2 pgoyette printf("\n\n"
164 1.5.2.2 pgoyette ">> %s, Revision %s (from NetBSD %s)\n",
165 1.5.2.2 pgoyette bootprog_name, bootprog_rev, bootprog_kernrev);
166 1.5.2.2 pgoyette }
167 1.5.2.2 pgoyette
168 1.5.2.2 pgoyette void
169 1.5.2.2 pgoyette boot(void)
170 1.5.2.2 pgoyette {
171 1.5.2.2 pgoyette int currname, c;
172 1.5.2.2 pgoyette
173 1.5.2.2 pgoyette print_banner();
174 1.5.2.2 pgoyette
175 1.5.2.2 pgoyette printf("Press return to boot now, any other key for boot prompt\n");
176 1.5.2.2 pgoyette for (currname = 0; currname < NUMNAMES; currname++) {
177 1.5.2.2 pgoyette printf("booting %s - starting in ", names[currname][0]);
178 1.5.2.2 pgoyette
179 1.5.2.2 pgoyette c = awaitkey(DEFTIMEOUT, 1);
180 1.5.2.2 pgoyette if ((c != '\r') && (c != '\n') && (c != '\0')) {
181 1.5.2.2 pgoyette bootprompt(); /* does not return */
182 1.5.2.2 pgoyette }
183 1.5.2.2 pgoyette
184 1.5.2.2 pgoyette /*
185 1.5.2.2 pgoyette * try pairs of names[] entries, foo and foo.gz
186 1.5.2.2 pgoyette */
187 1.5.2.2 pgoyette exec_netbsd(names[currname][0], "");
188 1.5.2.2 pgoyette exec_netbsd(names[currname][1], "");
189 1.5.2.2 pgoyette }
190 1.5.2.2 pgoyette
191 1.5.2.2 pgoyette bootprompt(); /* does not return */
192 1.5.2.2 pgoyette }
193