#include <sys/param.h>
#include <sys/types.h>
#include <machine/bootinfo.h>
#include <machine/disklabel.h>
#include <lib/libsa/stand.h>
#include <lib/libsa/loadfile.h>
#include "xxboot.h"
#include "libx68k.h"
#include "iocs.h"
#include "exec_image.h"
#define EXSCSI_BDID ((void *)0x00ea0001)
static int get_scsi_host_adapter(char *);
int get_scsi_part(void);
void bootmain(void) __attribute__ ((__noreturn__));
#if defined(XXBOOT_DEBUG)
void
print_hex(unsigned int x, int width)
{
if (width > 0) {
print_hex(x >> 4, width - 1);
x &= 0x0F;
if (x > 9)
x += 7;
IOCS_B_PUTC((unsigned int) '0' + x);
}
}
#endif
static int
get_scsi_host_adapter(char *devstr)
{
uint8_t *bootrom;
int ha;
#ifdef XXBOOT_DEBUG
*(uint32_t *)(devstr + 0) = '/' << 24 | 's' << 16 | 'p' << 8 | 'c';
#if defined(CDBOOT)
*(uint32_t *)(devstr + 4) = '@' << 24 | '0' << 16 | '/' << 8 | 'c';
#else
*(uint32_t *)(devstr + 4) = '@' << 24 | '0' << 16 | '/' << 8 | 's';
#endif
*(uint32_t *)(devstr + 8) = 'd' << 24 | '@' << 16 | '0' << 8 | ',';
*(uint32_t *)(devstr + 12) = '0' << 24 | ':' << 16 | 'a' << 8 | '\0';
#endif
bootrom = (uint8_t *)(BOOT_INFO & 0x00ffffe0);
if (*(uint16_t *)(bootrom + 0x24 + 4) == 0x494e) {
ha = (X68K_BOOT_SCSIIF_SPC << 4) | 0;
} else if (badbaddr(EXSCSI_BDID)) {
ha = (X68K_BOOT_SCSIIF_MHA << 4) | 0;
#ifdef XXBOOT_DEBUG
*(uint32_t *)devstr =
('/' << 24) | ('m' << 16) | ('h' << 8) | 'a';
#endif
} else {
ha = (X68K_BOOT_SCSIIF_SPC << 4) | 1;
#ifdef XXBOOT_DEBUG
devstr[5] = '1';
#endif
}
return ha;
}
#define PARTTBL_TOP (4)
#define NPART (15)
#define MAXPART (6)
int
get_scsi_part(void)
{
union {
char pad[1024];
struct {
uint32_t magic;
uint32_t parttotal;
uint32_t diskblocks;
uint32_t diskblocks2;
struct dos_partition parttbl[NPART];
} __packed;
} partbuf;
int i;
int part_top;
raw_read(PARTTBL_TOP, SCSI_CAP.blocksize, &partbuf);
if (partbuf.magic != 0x5836384b) {
BOOT_ERROR("Bad Human68k partition table");
}
part_top = SCSI_PARTTOP >> (2 - SCSI_BLKLEN);
for (i = 0; i < MAXPART; i++) {
if ((uint32_t)partbuf.parttbl[i].dp_start == part_top)
goto found;
}
BOOT_ERROR("Can't find this partition?");
found:
if (i >= 2)
i++;
return i;
}
void
bootmain(void)
{
int bootdev, fd;
char bootdevstr[16];
u_long marks[MARK_MAX];
IOCS_B_PRINT(bootprog_name);
IOCS_B_PRINT(" rev.");
IOCS_B_PRINT(bootprog_rev);
IOCS_B_PRINT("\r\n");
#if defined(XXBOOT_DEBUG)
int i;
for (i = 0; i < __arraycount(startregs); i++) {
print_hex(startregs[i], 8);
IOCS_B_PRINT((i & 7) == 7 ? "\r\n" : " ");
}
IOCS_B_PRINT("BOOT_INFO ");
print_hex(BOOT_INFO, 8);
IOCS_B_PRINT("\r\n");
#endif
if (BINF_ISFD(&BOOT_INFO)) {
int minor;
minor = (FDSEC.minsec.N == 3) ? 0 : 2;
bootdev = X68K_MAKEBOOTDEV(X68K_MAJOR_FD, BOOT_INFO & 3, minor);
#ifdef XXBOOT_DEBUG
*(uint32_t *)bootdevstr =
('f' << 24) | ('d' << 16) | ('@' << 8) |
('0' + (BOOT_INFO & 3));
bootdevstr[4] = 'a' + minor;
bootdevstr[5] = '\0';
#endif
} else {
int major, ha, part;
ha = get_scsi_host_adapter(bootdevstr);
part = 0;
#if defined(CDBOOT)
major = X68K_MAJOR_CD;
#else
major = X68K_MAJOR_SD;
if (SCSI_PARTTOP != 0)
part = get_scsi_part();
#endif
bootdev = X68K_MAKESCSIBOOTDEV(major, ha >> 4, ha & 15,
SCSI_ID & 7, 0, part);
#ifdef XXBOOT_DEBUG
bootdevstr[10] = '0' + (SCSI_ID & 7);
bootdevstr[14] = 'a' + part;
#endif
}
#ifdef XXBOOT_DEBUG
IOCS_B_PRINT("boot device: ");
IOCS_B_PRINT(bootdevstr);
#endif
IOCS_B_PRINT("\r\n");
marks[MARK_START] = BOOT_TEXTADDR;
#if defined(XXBOOT_USTARFS)
fd = loadfile("USTAR.volsize.4540", marks,
LOAD_TEXT|LOAD_DATA|LOAD_BSS);
#else
fd = loadfile("x68k/boot", marks, LOAD_TEXT|LOAD_DATA|LOAD_BSS);
if (fd < 0)
fd = loadfile("boot", marks, LOAD_TEXT|LOAD_DATA|LOAD_BSS);
#endif
if (fd >= 0) {
close(fd);
exec_image(BOOT_TEXTADDR,
BOOT_TEXTADDR,
BOOT_TEXTADDR,
0,
bootdev, 0);
}
IOCS_B_PRINT("can't load the secondary bootstrap.");
exit(0);
}
int
devopen(struct open_file *f, const char *fname, char **file)
{
*file = __UNCONST(fname);
return DEV_OPEN()(f);
}