[haiku-development] Re: booting from partition on flash drive

  • From: Caitlin Shaw <rogueeve@xxxxxxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Mon, 21 Sep 2009 15:08:27 -0700

>> Just wondering if it would be too much to ask for someone to produce  
>> makebootable binaries for linux and windows that could make a beos  
>> partition on a flash drive bootable? A standard MBR that can boot  
>> any OS in a primary partition would also be a nice touch.
>>     

My computer (an Acer Aspire 5720z) would not boot the image off the 
flash drive either, although there was no apparent reason why it should 
not. Perhaps that is why. Anyway, I used a Linux version of makebootable 
to get my haiku booted the first time. I don't know about a windows 
version, it would probably be 800 lines long ;), but if you use windows 
you can boot up off a linux livecd such as the one that comes with 
gparted, just cut and paste the source into a text file, "gcc 
makebootable.c -o makebootable" and you'll have a binary.

/*
 * Tiny version of Haiku's makebootable for Linux
 *
 * Copyright 2008, Stefan Schramm, mail@xxxxxxxxxxxxxxxxxx
 * Distributed under the terms of the MIT license.
 *
 * Changelog:
 * 2009-07-26: fixed compilation warnings due to bad printf-format
 *             usage (thanks to gordonjcp for pointing that out)
 * 2008-12-08: replaced example "hda4" with "hdaX" for safety
 *
 * Compiling: gcc makebootabletiny.c -o makebootabletiny
 */

#include <stdio.h>
#include <fcntl.h>
#include <linux/hdreg.h>

static const int partitionOffsetOffset = 506;

int main(int argc, const char *const *argv) {

    // check argument count
    if (argc < 2) {
        printf("Usage: %s <partition>\n", argv[0]);
        printf("Example: %s /dev/hdaX\n", argv[0]);
        return 1;
    }

    // open handle to partition
    int part = open(argv[1], O_RDWR);
    if (part < 0) {
        fprintf(stderr, "Error: Failed to open partition %s\n", argv[1]);
        return 1;
    }

    // get geometry
    struct hd_geometry geom;
    if (ioctl(part, HDIO_GETGEO, &geom) < 0) {
        fprintf(stderr, "Error: Failed to get device geometry\n");
        close(part);
        return 1;
    }

    // display geometry
    printf("determined partition start (sectors): 0x%lx\n", geom.start);

    // go to partition offset offset and read current configuration
    lseek(part, partitionOffsetOffset, 0);
    long buf = 0;
    read(part, &buf, 4);
    printf("old bootcode configuration:           0x%lx\n", buf);

    if (buf == geom.start) {
        printf("bootcode already configured correctly - exiting.\n");
        close(part);
        return 0;
    }

    // write partition offset
    lseek(part, partitionOffsetOffset, 0);
    write(part, &geom.start, 4);

    printf("bootcode configured - exiting.\n");
    close(part);
    return 0;

}



Other related posts: