qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Jim C. Brown" <jma5@umd.edu>
To: Fabrice Bellard <fabrice@bellard.org>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] using partition images
Date: Mon, 8 May 2006 08:26:20 -0400	[thread overview]
Message-ID: <20060508122620.GA19236@jbrown.mylinuxbox.org> (raw)
In-Reply-To: <445F1B6C.70205@bellard.org>

[-- Attachment #1: Type: text/plain, Size: 1137 bytes --]

On Mon, May 08, 2006 at 12:20:28PM +0200, Fabrice Bellard wrote:
> A few ideas:
> 
> - Use an external file 'bootsect.bin' as it is done for linux_boot.bin.

Done. I've decided to use the name bootmbr.bin because I think that name
describes its function more accurately.

> - Provide the source code of the boot sector.

After realizing that the original bootsector was actually a dump from an MSDOS
disk (and thus probably closed source) I decided to use BOOTNORM.ASM

BOOTNORM.ASM is from the FreeDISK FDISK (available http://www.23cc.com/free-fdisk and http://ffdisk.webaps.de/fdisk121.zip) and is GPL.

> - Instead of copying the raw block driver, use the block driver recursively.

I'll work on this tonight. I've been thinking about doing this, since it would
allow one to use any qemu-supported disk image format as a partition image.

I can't think of any disk format that's heavily used in qemu that is normally
used for partition images except for raw. OTOH it might be interesting to have
qcow partition images.

> 
> Fabrice.
> 

-- 
Infinite complexity begets infinite beauty.
Infinite precision begets infinite perfection.

[-- Attachment #2: block-part-raw.c --]
[-- Type: text/plain, Size: 7063 bytes --]

/*
 * Block driver to use partition images instead of whole hard disk images
 * 
 * Copyright (c) 2007 Jim Brown
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vl.h"
#include "block_int.h"

#ifdef __sun__
#include <sys/dkio.h>
#endif

typedef struct BDRVPartRawState {
    char mbr_data[63*512];
    int fd;
} BDRVPartRawState;

static int part_raw_probe(const uint8_t *buf, int buf_size, const char *filename)
{
    if (strstart(filename, "part:", NULL))
        return 100;
    return 0;
}

static int part_raw_open(BlockDriverState *bs, const char *nfilename)
{
    BDRVPartRawState *s = bs->opaque;
    int fd, boot_fd;
    int64_t size;
#ifdef _BSD
    struct stat sb;
#endif
#ifdef __sun__
    struct dk_minfo minfo;
    int rv;
#endif
    int head, cylinder, sector;
    const char * filename = &(nfilename[5]);

    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
    if (fd < 0) {
        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
        if (fd < 0)
            return -1;
        bs->read_only = 1;
    }
#ifdef _BSD
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
#ifdef DIOCGMEDIASIZE
	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
#endif
#ifdef CONFIG_COCOA
        size = LONG_LONG_MAX;
#else
        size = lseek(fd, 0LL, SEEK_END);
#endif
    } else
#endif
#ifdef __sun__
    /*
     * use the DKIOCGMEDIAINFO ioctl to read the size.
     */
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
    if ( rv != -1 ) {
        size = minfo.dki_lbsize * minfo.dki_capacity;
    } else /* there are reports that lseek on some devices
              fails, but irc discussion said that contingency
              on contingency was overkill */
#endif
    {
        size = lseek(fd, 0, SEEK_END);
    }
    bs->total_sectors = (size / 512) + 63;
    s->fd = fd;

    /* set up c/h/s */
    size = size+(63*512);
    cylinder = size/(63*16);
    /* FIXME */
    cylinder = cylinder + 1; /* add a cylinder just in case partition extends beyond the edge of the last cylinder/head/track */
    head = 16;
    sector = 63;
    /* some bit twiddling here */
    sector = (((cylinder >> 8) & 3) << 6) + sector;

    /* set up fake MBR */
    memset(s->mbr_data, 0, 63*512);
    boot_fd = open("bootmbr.bin", O_RDONLY);
    if (boot_fd == -1)
    {
    printf("Warning: failed to open bootsector.bin - MBR will not be bootbale\n");
    }
    else
    {
    	if (read(boot_fd, s->mbr_data, 512) == -1)
	{
    printf("Warning: failed to read bootsector.bin - MBR will not be bootbale\n");
	}
    	close(boot_fd);
    }
    /* first partition is bootable */
    s->mbr_data[446] = 0x80;
    /* start head */
    s->mbr_data[447] = 0x01;
    /* start sector - only first 6 bits */
    s->mbr_data[448] = 0x01;
    /* start cylinder - this byte plus 2 bits from mbr_data[447] */
    s->mbr_data[449] = 0x00;
    /* system ID */
    s->mbr_data[450] = 0x0C; /* say we're win98 fat32 */
    /* ending head */
    s->mbr_data[451] = head;
    /* ending sector */
    s->mbr_data[452] = sector;
    /* ending cylinder */
    s->mbr_data[453] = cylinder;
    /* absolute start sector - 4 bytes/DWORD */
    s->mbr_data[454] = 0x3F; // 3F = 63
    /* absolute total number of sectors - 4 bytes/DWORD */
    *((uint32_t*)(s->mbr_data+458)) = cpu_to_le32(bs->total_sectors - 63);
    /* leave the other partitions blank - we only support the first one */

    /* set the MBR sector signature */
    s->mbr_data[510] = 0x55;
    s->mbr_data[511] = 0xAA;

    return 0;
}

static int part_raw_read(BlockDriverState *bs, int64_t sector_num, 
                    uint8_t *buf, int nb_sectors)
{
    BDRVPartRawState *s = bs->opaque;
    int ret,split;

    if (sector_num >= 63)
    {
    
    lseek(s->fd, (sector_num - 63) * 512, SEEK_SET);
    ret = read(s->fd, buf, nb_sectors * 512);
    if (ret != nb_sectors * 512) 
        return -1;
    return 0;

    }
    else
    {

    if ((nb_sectors + sector_num) > 63)
    {
    	/* ah hell - we have to do both the fake part and the real part */

	split = nb_sectors + sector_num - 63;
	ret = part_raw_read(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
	if (ret != split * 512)
	    return -1;

	/* this will always return 0 */
	ret = part_raw_read(bs, sector_num, buf, (nb_sectors - split) * 512);
	return 0;
    }
    else
    {
    	memcpy(buf, &(s->mbr_data[sector_num*512]), nb_sectors*512);
	return 0;
    }

    }
}

static int part_raw_write(BlockDriverState *bs, int64_t sector_num, 
                     const uint8_t *buf, int nb_sectors)
{
    BDRVPartRawState *s = bs->opaque;
    int ret, split;

    if (sector_num >= 63)
    {
    
    lseek(s->fd, (sector_num - 63) * 512, SEEK_SET);
    ret = write(s->fd, buf, nb_sectors * 512);
    if (ret != nb_sectors * 512) 
        return -1;
    return 0;

    }
    else
    {

    if ((nb_sectors + sector_num) > 63)
    {
    	/* ah hell - we have to do both the fake part and the real part */

	split = nb_sectors + sector_num - 63;
	ret = part_raw_write(bs, 63, &buf[(nb_sectors-split)*512], split * 512);
	if (ret != split * 512)
	    return -1;

	/* this will always return 0 */
	ret = part_raw_write(bs, sector_num, buf, (nb_sectors - split) * 512);
	return 0;
    }
    else
    {
    	memcpy(&(s->mbr_data[sector_num*512]), buf, nb_sectors*512);
	return 0;
    }

    }
}

static void part_raw_close(BlockDriverState *bs)
{
    BDRVPartRawState *s = bs->opaque;
    close(s->fd);
}

static int part_raw_create(const char *filename, int64_t total_size,
                      const char *backing_file, int flags)
{
    int fd;

    if (flags || backing_file)
        return -ENOTSUP;

    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
              0644);
    if (fd < 0)
        return -EIO;
    ftruncate(fd, total_size * 512);
    close(fd);
    return 0;
}

BlockDriver bdrv_part_raw = {
    "part_raw",
    sizeof(BDRVPartRawState),
    part_raw_probe,
    part_raw_open,
    part_raw_read,
    part_raw_write,
    part_raw_close,
    part_raw_create,
};


[-- Attachment #3: bootmbr.bin --]
[-- Type: application/octet-stream, Size: 512 bytes --]

[-- Attachment #4: BOOTNORM.ASM --]
[-- Type: text/plain, Size: 5809 bytes --]

;

; normal DOS boot sector

;





segment _DATA           class=DATA align=2



                 

  global  _bootnormal_code

_bootnormal_code:



;-----------------------------------------------------------------------

;   ENTRY (copied from freedos bootsector)

;

; IN: DL = boot drive

;OUT: DL = boot drive

;

;-----------------------------------------------------------------------



real_start:     cli

                cld

                xor     ax, ax

                mov     ss, ax          ; initialize stack

                mov     ds, ax

                mov     bp, 0x7c00

                lea     sp, [bp-0x20]

                sti

                

                mov     ax, 0x1FE0

                mov     es, ax

                mov     si, bp

                mov     di, bp

                mov     cx, 0x0100

                rep     movsw



                jmp     word 0x1FE0:0x7c00+ cont-real_start



cont:           mov     ds, ax

                mov     ss, ax

                xor     ax,ax

                mov     es,ax



;               call    print

;               db      "FreeDOS MBR...",0





										 ; search for active partition

                lea di, [bp+0x1be] ; start of partition table

test_next_for_active:				

                test byte [di],0x80

                jne  active_partition_found

                add  di,0x10                    ; next table

                cmp  di, 07c00h+0x1fe; scanned beyond end of table ??

                jb  test_next_for_active



;*****************************************************************				

                call print

                db 'no active partition found',0

				

WAIT_FOR_REBOOT:

                jmp $





;*****************************************************************

trouble_reading_drive:

                call print

                db 'read error while reading drive',0

                jmp WAIT_FOR_REBOOT



;*****************************************************************



invalid_partition_code:

                call print

                db 'partition signature != 55AA',0

			

                jmp WAIT_FOR_REBOOT



								

;*****************************************************************



active_partition_found:

;				call print

;				db 'loading active partition',0



                call read_boot_sector                    

				

                jc  trouble_reading_drive

			

                cmp word [es:0x7c00+0x1fe],0xaa55

                jne invalid_partition_code

			

;               call print

;               db '.jump DOS..',0

			

                jmp word 0x0:0x7c00             ; and jump to boot sector code





;*****************************

; read_boot_sector				

;

; IN: DI--> partition info

;OUT:CARRY

;*****************************



read_boot_sector:

                ;  /* check for LBA support */

		mov bx,0x55aa		

		mov ah,0x41

		int 0x13



		jc  StandardBios    ;  if (regs.b.x != 0xaa55 || (regs.flags & 0x01))

		cmp bx,0xaa55       ;    goto StandardBios;

		jne StandardBios



                              ;  /* if DAP cannot be used, don't use LBA */

                              ;  if ((regs.c.x & 1) == 0)

                              ;    goto StandardBios;

 		test cl,1

 		jz StandardBios

 

        jmp short LBABios







;struct _bios_LBA_address_packet            /* Used to access a hard disk via LBA */

;{

;  unsigned char packet_size;    /* size of this packet...set to 16  */

;  unsigned char reserved_1;     /* set to 0...unused                */

;  unsigned char number_of_blocks;       /* 0 < number_of_blocks < 128       */

;  unsigned char reserved_2;     /* set to 0...unused                */

;  UBYTE far *buffer_address;    /* addr of transfer buffer          */

;  unsigned long block_address;  /* LBA address                      */

;  unsigned long block_address_high;     /* high bytes of LBA addr...unused  */

;};



_bios_LBA_address_packet:

	db 0x10

	db 0

	db 4				; read four sectors - why not

	db 0

	dw 0x7c00			; fixed boot address for DOS sector

	dw 0x0000	

_bios_LBA_low  dw 0

_bios_LBA_high dw 0

	dw 0,0





LBABios:

						; copy start address of partition to DAP

	mov ax,[di+8]

	mov [0x7c00+ (_bios_LBA_low-real_start)],ax

	mov ax,[di+8+2]

	mov [0x7c00+ (_bios_LBA_high-real_start)],ax



    mov ax,0x4200		;  regs.a.x = LBA_READ;

    mov si,0x7c00+ (_bios_LBA_address_packet-real_start); regs.si = FP_OFF(&dap);



	int 0x13

	ret



;*****************************************************************

; read disk, using standard BIOS

;

StandardBios:

;	call print

;	db 'standard BIOS',0  





    mov ax,0x0204			;  regs.a.x = 0x0201;

    mov bx,0x7c00			;  regs.b.x = FP_OFF(buffer);

	mov cx,[di+2]			;      regs.c.x =

          					; ((chs.Cylinder & 0xff) << 8) + ((chs.Cylinder & 0x300) >> 2) +

          					; chs.Sector;

          					; that was easy ;-)

    mov dh,[di+1]			;  regs.d.b.h = chs.Head;

    						;  regs.es = FP_SEG(buffer);

	int 0x13

	ret	

	





;****** PRINT

; prints text after call to this function.



print_1char:        

                xor   bx, bx                   ; video page 0

                mov   ah, 0x0E                 ; else print it

                int   0x10                     ; via TTY mode

print:          pop   si                       ; this is the first character

print1:         lodsb                          ; get token

                push  si                       ; stack up potential return address

                cmp   al, 0                    ; end of string?

                jne   print_1char              ; until done

                ret                            ; and jump to it







		times	0x1fe-$+$$ db 0

		db 0x55,0xaa

		


  reply	other threads:[~2006-05-08 12:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-08  3:53 [Qemu-devel] using partition images Jim C. Brown
2006-05-08  4:49 ` Jim C. Brown
2006-05-08 10:20   ` Fabrice Bellard
2006-05-08 12:26     ` Jim C. Brown [this message]
2006-05-08 12:32       ` Johannes Schindelin
2006-05-08 13:11       ` Paul Brook
2006-05-08 14:19         ` Jim C. Brown
2006-05-08 14:28           ` Paul Brook
2006-05-08 14:59             ` Jim C. Brown
2006-05-08 15:09               ` Paul Brook
2006-05-08 14:49       ` Jim C. Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060508122620.GA19236@jbrown.mylinuxbox.org \
    --to=jma5@umd.edu \
    --cc=fabrice@bellard.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).