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