1 // Code for emulating a drive via high-memory accesses.
3 // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "biosvar.h" // GET_GLOBALFLAT
8 #include "block.h" // struct drive_s
9 #include "bregs.h" // struct bregs
10 #include "e820map.h" // e820_add
11 #include "malloc.h" // memalign_tmphigh
12 #include "memmap.h" // PAGE_SIZE
13 #include "output.h" // dprintf
14 #include "romfile.h" // romfile_findprefix
15 #include "stacks.h" // call16_int
16 #include "std/disk.h" // DISK_RET_SUCCESS
17 #include "string.h" // memset
18 #include "util.h" // process_ramdisk_op
23 if (!CONFIG_FLASH_FLOPPY)
27 struct romfile_s *file = romfile_findprefix("floppyimg/", NULL);
30 const char *filename = file->name;
31 u32 size = file->size;
32 dprintf(3, "Found floppy file %s of size %d\n", filename, size);
33 int ftype = find_floppy_type(size);
35 dprintf(3, "No floppy type found for ramdisk size\n");
39 // Allocate ram for image.
40 void *pos = memalign_tmphigh(PAGE_SIZE, size);
45 e820_add((u32)pos, size, E820_RESERVED);
47 // Copy image into ram.
48 int ret = file->copy(file, pos, size);
53 struct drive_s *drive = init_floppy((u32)pos, ftype);
56 drive->type = DTYPE_RAMDISK;
57 dprintf(1, "Mapping floppy %s to addr %p\n", filename, pos);
58 char *desc = znprintf(MAXDESCSIZE, "Ramdisk [%s]", &filename[10]);
59 boot_add_floppy(drive, desc, bootprio_find_named_rom(filename, 0));
63 ramdisk_copy(struct disk_op_s *op, int iswrite)
65 u32 offset = GET_GLOBALFLAT(op->drive_gf->cntl_id);
66 offset += (u32)op->lba * DISK_SECTOR_SIZE;
67 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
68 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
79 // Call int 1587 to copy data.
81 memset(&br, 0, sizeof(br));
86 br.cx = op->count * DISK_SECTOR_SIZE / 2;
87 call16_int(0x15, &br);
90 return DISK_RET_EBADTRACK;
91 return DISK_RET_SUCCESS;
95 ramdisk_process_op(struct disk_op_s *op)
97 if (!CONFIG_FLASH_FLOPPY)
100 switch (op->command) {
102 return ramdisk_copy(op, 0);
104 return ramdisk_copy(op, 1);
106 return default_process_op(op);