1 /* $NetBSD: main.c,v 1.23 2008/07/15 21:29:37 perry Exp $ */ 2 3 /* 4 * Copyright (c) 1996, 1997 5 * Matthias Drochner. All rights reserved. 6 * Copyright (c) 1996, 1997 7 * Perry E. Metzger. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgements: 19 * This product includes software developed for the NetBSD Project 20 * by Matthias Drochner. 21 * This product includes software developed for the NetBSD Project 22 * by Perry E. Metzger. 23 * 4. The names of the authors may not be used to endorse or promote products 24 * derived from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 39 #include <sys/reboot.h> 40 41 #include <lib/libkern/libkern.h> 42 #include <lib/libsa/stand.h> 43 #include <lib/libsa/ufs.h> 44 45 #include <libi386.h> 46 47 #ifdef SUPPORT_LYNX 48 extern int exec_lynx(const char*, int); 49 #endif 50 51 int errno; 52 53 extern char bootprog_name[], bootprog_rev[], bootprog_kernrev[]; 54 55 #define MAXDEVNAME 16 56 57 static char *current_fsmode; 58 static char *default_devname; 59 static int default_unit, default_partition; 60 static char *default_filename; 61 62 char *sprint_bootsel(const char *); 63 static void bootit(const char *, int, int); 64 void usage(void); 65 int main(int, char **); 66 67 void command_help(char *); 68 void command_ls(char *); 69 void command_quit(char *); 70 void command_boot(char *); 71 void command_mode(char *); 72 void command_dev(char *); 73 74 const struct bootblk_command commands[] = { 75 { "help", command_help }, 76 { "?", command_help }, 77 { "ls", command_ls }, 78 { "quit", command_quit }, 79 { "boot", command_boot }, 80 { "mode", command_mode }, 81 { "dev", command_dev }, 82 { NULL, NULL }, 83 }; 84 85 int 86 parsebootfile(fname, fsmode, devname, unit, partition, file) 87 const char *fname; 88 char **fsmode; /* out */ 89 char **devname; /* out */ 90 int *unit, *partition; /* out */ 91 const char **file; /* out */ 92 { 93 const char *col, *help; 94 95 *fsmode = current_fsmode; 96 *devname = default_devname; 97 *unit = default_unit; 98 *partition = default_partition; 99 *file = default_filename; 100 101 if (fname == NULL) 102 return (0); 103 104 if (strcmp(current_fsmode, "dos") && (col = strchr(fname, ':'))) { 105 /* no DOS, device given */ 106 static char savedevname[MAXDEVNAME + 1]; 107 int devlen; 108 unsigned int u = 0, p = 0; 109 int i = 0; 110 111 devlen = col - fname; 112 if (devlen > MAXDEVNAME) 113 return (EINVAL); 114 115 #define isvalidname(c) ((c) >= 'a' && (c) <= 'z') 116 if (!isvalidname(fname[i])) 117 return (EINVAL); 118 do { 119 savedevname[i] = fname[i]; 120 i++; 121 } while (isvalidname(fname[i])); 122 savedevname[i] = '\0'; 123 124 #define isnum(c) ((c) >= '0' && (c) <= '9') 125 if (i < devlen) { 126 if (!isnum(fname[i])) 127 return (EUNIT); 128 do { 129 u *= 10; 130 u += fname[i++] - '0'; 131 } while (isnum(fname[i])); 132 } 133 134 #define isvalidpart(c) ((c) >= 'a' && (c) <= 'z') 135 if (i < devlen) { 136 if (!isvalidpart(fname[i])) 137 return (EPART); 138 p = fname[i++] - 'a'; 139 } 140 if (i != devlen) 141 return (ENXIO); 142 143 *devname = savedevname; 144 *unit = u; 145 *partition = p; 146 help = col + 1; 147 } else 148 help = fname; 149 150 if (*help) 151 *file = help; 152 153 return (0); 154 } 155 156 char * 157 sprint_bootsel(filename) 158 const char *filename; 159 { 160 char *fsname, *devname; 161 int unit, partition; 162 const char *file; 163 static char buf[80]; 164 165 if (parsebootfile(filename, &fsname, &devname, &unit, 166 &partition, &file) == 0) { 167 if (!strcmp(fsname, "dos")) 168 sprintf(buf, "dos:%s", file); 169 else if (!strcmp(fsname, "ufs")) 170 sprintf(buf, "%s%d%c:%s", devname, unit, 171 'a' + partition, file); 172 else goto bad; 173 return (buf); 174 } 175 bad: 176 return ("(invalid)"); 177 } 178 179 static void 180 bootit(filename, howto, tell) 181 const char *filename; 182 int howto, tell; 183 { 184 if (tell) { 185 printf("booting %s", sprint_bootsel(filename)); 186 if (howto) 187 printf(" (howto 0x%x)", howto); 188 printf("\n"); 189 } 190 #ifdef SUPPORT_LYNX 191 if(exec_netbsd(filename, 0, howto) < 0) 192 printf("boot netbsd: %s: %s\n", sprint_bootsel(filename), 193 strerror(errno)); 194 else { 195 printf("boot netbsd returned\n"); 196 return; 197 } 198 if (exec_lynx(filename, 0) < 0) 199 printf("boot lynx: %s: %s\n", sprint_bootsel(filename), 200 strerror(errno)); 201 else 202 printf("boot lynx returned\n"); 203 #else 204 if (exec_netbsd(filename, 0, howto) < 0) 205 printf("boot: %s: %s\n", sprint_bootsel(filename), 206 strerror(errno)); 207 else 208 printf("boot returned\n"); 209 #endif 210 } 211 212 static void 213 print_banner(void) 214 { 215 int extmem = getextmem(); 216 char *s = ""; 217 218 #ifdef XMS 219 u_long xmsmem; 220 if (getextmem1() == 0 && (xmsmem = checkxms()) != 0) { 221 /* 222 * With "CONSERVATIVE_MEMDETECT", extmem is 0 because 223 * getextmem() is getextmem1(). Without, the "smart" 224 * methods could fail to report all memory as well. 225 * xmsmem is a few kB less than the actual size, but 226 * better than nothing. 227 */ 228 if ((int)xmsmem > extmem) 229 extmem = xmsmem; 230 s = "(xms) "; 231 } 232 #endif 233 234 printf("\n" 235 ">> %s, Revision %s (from NetBSD %s)\n" 236 ">> Memory: %d/%d %sk\n", 237 bootprog_name, bootprog_rev, bootprog_kernrev, 238 getbasemem(), extmem, s); 239 } 240 241 void 242 usage() 243 { 244 printf("dosboot [-u] [-c <commands>] [-i] [filename [-bootopts]]\n"); 245 } 246 247 int 248 main(argc, argv) 249 int argc; 250 char **argv; 251 { 252 int ch; 253 int interactive = 0; 254 int howto; 255 extern char *optarg; 256 extern int optind; 257 258 #ifdef SUPPORT_SERIAL 259 initio(SUPPORT_SERIAL); 260 #else 261 initio(CONSDEV_PC); 262 #endif 263 gateA20(); 264 265 print_banner(); 266 267 current_fsmode = "dos"; 268 default_devname = "hd"; 269 default_unit = 0; 270 default_partition = 0; 271 default_filename = "netbsd"; 272 273 while ((ch = getopt(argc, argv, "c:iu")) != -1) { 274 switch (ch) { 275 case 'c': 276 docommand(optarg); 277 return (1); 278 break; 279 case 'i': 280 interactive = 1; 281 break; 282 case 'u': 283 current_fsmode = "ufs"; 284 break; 285 default: 286 usage(); 287 return (1); 288 } 289 } 290 291 if (interactive) { 292 printf("type \"?\" or \"help\" for help.\n"); 293 bootmenu(); 294 } 295 296 argc -= optind; 297 argv += optind; 298 299 if (argc > 2) { 300 usage(); 301 return (1); 302 } 303 howto = 0; 304 if (argc > 1 && !parseopts(argv[1], &howto)) 305 return (1); 306 307 bootit((argc > 0 ? argv[0] : "netbsd"), howto, 1); 308 return (1); 309 } 310 311 /* ARGSUSED */ 312 void 313 command_help(arg) 314 char *arg; 315 { 316 printf("commands are:\n" 317 "boot [xdNx:][filename] [-acdqsv]\n" 318 " (ex. \"sd0a:netbsd.old -s\"\n" 319 "ls [path]\n" 320 "mode ufs|dos\n" 321 "dev xd[N[x]]:\n" 322 "help|?\n" 323 "quit\n"); 324 } 325 326 void 327 command_ls(arg) 328 char *arg; 329 { 330 char *help = default_filename; 331 if (strcmp(current_fsmode, "ufs")) { 332 printf("UFS only\n"); 333 return; 334 } 335 default_filename = "/"; 336 ufs_ls(arg); 337 default_filename = help; 338 } 339 340 /* ARGSUSED */ 341 void 342 command_quit(arg) 343 char *arg; 344 { 345 printf("Exiting... goodbye...\n"); 346 exit(0); 347 } 348 349 void 350 command_boot(arg) 351 char *arg; 352 { 353 char *filename; 354 int howto; 355 356 if (parseboot(arg, &filename, &howto)) 357 bootit(filename, howto, 1); 358 } 359 360 void 361 command_mode(arg) 362 char *arg; 363 { 364 if (!strcmp("dos", arg)) 365 current_fsmode = "dos"; 366 else if (!strcmp("ufs", arg)) 367 current_fsmode = "ufs"; 368 else 369 printf("invalid mode\n"); 370 } 371 372 void 373 command_dev(arg) 374 char *arg; 375 { 376 static char savedevname[MAXDEVNAME + 1]; 377 char *fsname, *devname; 378 const char *file; /* dummy */ 379 380 if (!strcmp(current_fsmode, "dos")) { 381 printf("not available in DOS mode\n"); 382 return; 383 } 384 385 if (*arg == '\0') { 386 printf("%s%d%c:\n", default_devname, default_unit, 387 'a' + default_partition); 388 return; 389 } 390 391 if (!strchr(arg, ':') || 392 parsebootfile(arg, &fsname, &devname, &default_unit, 393 &default_partition, &file)) { 394 command_help(NULL); 395 return; 396 } 397 398 /* put to own static storage */ 399 strncpy(savedevname, devname, MAXDEVNAME + 1); 400 default_devname = savedevname; 401 } 402