boot2.c revision 199767f8919635c4928607450d9e0abb932109ce
/*-
* Copyright (c) 2008 John Hay
* Copyright (c) 1998 Robert Nordier
* All rights reserved.
*
* Redistribution and use in source and binary forms are freely
* permitted provided that the above copyright notice and this
* paragraph and the following disclaimer are duplicated in all
* such forms.
*
* This software is provided "AS IS" and without any express or
* implied warranties, including, without limitation, the implied
* warranties of merchantability and fitness for a particular
* purpose.
*/
__FBSDID("$FreeBSD$");
#include <sys/disklabel.h>
#include <stdarg.h>
#include "lib.h"
#include "paths.h"
#include "rbx.h"
#define NOPT 6
};
static unsigned dsk_start;
static char cmd[512];
static char kname[1024];
static int bootslice;
static int bootpart;
static int disk_layout;
#define DL_UNKNOWN 0
static void load(void);
static int parse(void);
static int dskread(void *, unsigned, unsigned);
static int drvread(void *, unsigned, unsigned);
#ifdef FIXUP_BOOT_DRV
static void fixup_boot_drv(caddr_t, int, int, int);
#endif
#include "ufsread.c"
#ifdef DEBUG
#else
#endif
static inline int
{
return -1;
return 0;
}
static inline void
getstr(int c)
{
char *s;
s = cmd;
if (c == 0)
c = getc(10000);
for (;;) {
switch (c) {
case 0:
break;
case '\177':
case '\b':
if (s > cmd) {
s--;
printf("\b \b");
}
break;
case '\n':
case '\r':
*s = 0;
return;
default:
*s++ = c;
xputchar(c);
}
c = getc(10000);
}
}
int
main(void)
{
const char *bt;
int autoboot, c = 0;
dmadat = (void *)(0x1c0000);
bt = board_init();
autoboot = 1;
/* Process configuration file */
if (*cmd) {
if (parse())
autoboot = 0;
/* Do not process this command twice */
*cmd = 0;
}
if (*kname == '\0')
/* Present the user with the boot2 prompt. */
for (;;) {
if (!autoboot ||
getstr(c);
xputchar('\n');
autoboot = 0;
c = 0;
if (parse())
xputchar('\a');
else
load();
}
}
static void
load(void)
{
caddr_t p;
int i, j;
#ifdef FIXUP_BOOT_DRV
int klen;
klen = 0;
#endif
if (!ls)
return;
}
return;
return;
}
return;
j++;
}
for (i = 0; i < 2; i++) {
#ifdef FIXUP_BOOT_DRV
staddr = p;
#endif
return;
}
clr_board();
#ifdef FIXUP_BOOT_DRV
#endif
}
static int
parse()
{
char *ep, *p;
int c, i;
while ((c = *arg++)) {
if (c == ' ' || c == '\t' || c == '\n')
continue;
ep = p;
if (*p)
*p++ = 0;
if (c == '-') {
while ((c = *arg++)) {
for (i = 0; c != optstr[i]; i++)
if (i == NOPT - 1)
return -1;
}
} else {
arg--;
/* look for ad0s1a:... | ad0s1:... */
/* XXX Should also handle disk. */
return (-1);
bootpart = 0;
return (-1);
dsk_meta = 0;
arg += 6;
else
arg += 7;
/* look for ad0a:... */
bootslice = 0;
return (-1);
dsk_meta = 0;
arg += 5;
}
return -1;
}
}
arg = p;
}
return 0;
}
/*
* dskread() will try to handle the disk layouts that are typically
* encountered.
* - raw or "Dangerously Dedicated" mode. No real slice table, just the
* default one that is included with bsdlabel -B. Typically this is
* used with ROOTDEVNAME=\"ufs:ad0a\".
* - slice only. Only a slice table is installed with no bsd label or
* bsd partition table. This is typically used with
* ROOTDEVNAME=\"ufs:ad0s1\".
* - slice + bsd label + partition table. This is typically done with
* with fdisk + bsdlabel and is used with ROOTDEVNAME=\"ufs:ad0s1a\".
*/
static int
{
struct dos_partition *dp;
struct disklabel *d;
char *sec;
int i;
if (!dsk_meta) {
dsk_start = 0;
return -1;
if (bootslice != 0) {
i = bootslice - 1;
return -1;
} else {
for (i = 0; i < NDOSPART; i++) {
break;
}
}
if (i != NDOSPART) {
bootslice = i + 1;
/*
* Although dp_start is aligned within the disk
* partition structure, DOSPARTOFF is 446, which
* is only word (2) aligned, not longword (4)
* aligned. Cope by using memcpy to fetch the
* start of this partition.
*/
bootslice = 0;
}
}
return -1;
d = (void *)(sec + LABELOFFSET);
DPRINTF("p_size = %x\n",
printf("Invalid partition\n");
return -1;
}
DPRINTF("p_offset %x, RAW %x\n",
else if (disk_layout == DL_UNKNOWN)
} else {
}
dsk_meta++;
}
}
static int
{
static unsigned c = 0x2d5c7c2f;
}
#ifdef FIXUP_BOOT_DRV
/*
* fixup_boot_drv() will try to find the ROOTDEVNAME spec in the kernel
* and change it to what was specified on the comandline or /boot.conf
* file or to what was encountered on the disk. It will try to handle 3
* different disk layouts, raw (dangerously dedicated), slice only and
* slice + partition. It will look for the following strings in the
* kernel, but if it is one of the first three, the string in the kernel
* must use the correct form to match the actual disk layout:
* - ufs:ad0a
* - ufs:ad0s1
* - ufs:ad0s1a
* - ufs:ROOTDEVNAME
* In the case of the first three strings, only the "a" at the end and
* the "1" after the "s" will be modified, if they exist. The string
* length will not be changed. In the case of the last string, the
* whole string will be built up and nul, '\0' terminated.
*/
static void
{
DPRINTF("fixup_boot_drv: 0x%x, %d, slice %d, partition %d\n",
if (bs > 4)
return;
if (bp > 7)
return;
p[0] = 'a'; p[1] = 'd'; p[2] = '0'; /* ad0 */
p += 3;
if (bs > 0) {
/* append slice */
*p++ = 's';
*p++ = bs + '0';
}
if (disk_layout != DL_SLICE) {
/* append partition */
*p++ = bp + 'a';
}
*p = '\0';
} else {
if (*p == 's') {
/* fix slice */
p++;
*p++ = bs + '0';
}
if (*p == 'a')
*p = bp + 'a';
}
}
printf("Could not locate \"%s\" to fix kernel boot device, "
"check ROOTDEVNAME is set\n", op);
return;
}
}
#endif