199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This file and its contents are supplied under the terms of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Common Development and Distribution License ("CDDL"), version 1.0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You may only use this file in accordance with the terms of version
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1.0 of the CDDL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A full copy of the text of the CDDL should have accompanied this
199767f8919635c4928607450d9e0abb932109ceToomas Soome * source. A copy of the CDDL is also available via the Internet at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * http://www.illumos.org/license/CDDL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright 2015 Toomas Soome <tsoome@me.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Chain loader to load BIOS boot block either from MBR or PBR.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note the boot block location 0000:7c000 conflicts with loader, so we need to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * read in to temporary space and relocate on exec, when btx is stopped.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/linker.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/diskmbr.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libi386/libi386.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "btxv86.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The MBR/VBR is located in first sector of disk/partition.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Read 512B to temporary location and set up relocation. Then
199767f8919635c4928607450d9e0abb932109ceToomas Soome * exec relocator.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SECTOR_SIZE (512)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(chain, "chain", "chain load boot block from device", command_chain);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_chain(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int fd, len, size = SECTOR_SIZE;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct stat st;
199767f8919635c4928607450d9e0abb932109ceToomas Soome vm_offset_t mem = 0x100000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint32_t *uintptr = &relocater_data;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct i386_devdesc *rootdev;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc == 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "no device or file name specified";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argc != 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "invalid trailing arguments";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome fd = open(argv[1], O_RDONLY);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fd == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "open failed";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome len = strlen(argv[1]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argv[1][len-1] != ':') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (fstat(fd, &st) == -1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "stat failed";
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = st.st_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (strncmp(argv[1], "disk", 4) != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "can only use disk device";
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome i386_getdev((void **)(&rootdev), argv[1], NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rootdev == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "can't determine root device";
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (archsw.arch_readin(fd, mem, size) != size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "failed to read disk";
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome close(fd);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (argv[1][len-1] == ':' &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome *((uint16_t *)PTOV(mem + DOSMAGICOFFSET)) != DOSMAGIC) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome command_errmsg = "wrong magic";
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome uintptr[0] = mem;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uintptr[1] = 0x7C00;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uintptr[2] = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_edx = bd_unit2bios(rootdev->d_unit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_esi = relocater_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_ds = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_es = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_fs = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_gs = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_ss = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_cs = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_sp = 0x7C00;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_ip = 0x7C00;
199767f8919635c4928607450d9e0abb932109ceToomas Soome relocator_a20_enabled = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome i386_copyin(relocater, 0x600, relocater_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dev_cleanup();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome __exec((void *)0x600);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome panic("exec returned");
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_ERROR); /* not reached */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}