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 "malloc.h" // malloc_fseg
11 #include "memmap.h" // add_e820
12 #include "output.h" // dprintf
13 #include "romfile.h" // romfile_findprefix
14 #include "stacks.h" // call16_int
15 #include "std/disk.h" // DISK_RET_SUCCESS
16 #include "string.h" // memset
17 #include "util.h" // process_ramdisk_op
22 if (!CONFIG_FLASH_FLOPPY)
26 struct romfile_s *file = romfile_findprefix("floppyimg/", NULL);
29 const char *filename = file->name;
30 u32 size = file->size;
31 dprintf(3, "Found floppy file %s of size %d\n", filename, size);
32 int ftype = find_floppy_type(size);
34 dprintf(3, "No floppy type found for ramdisk size\n");
38 // Allocate ram for image.
39 void *pos = memalign_tmphigh(PAGE_SIZE, size);
44 add_e820((u32)pos, size, E820_RESERVED);
46 // Copy image into ram.
47 int ret = file->copy(file, pos, size);
52 struct drive_s *drive = init_floppy((u32)pos, ftype);
55 drive->type = DTYPE_RAMDISK;
56 dprintf(1, "Mapping CBFS floppy %s to addr %p\n", filename, pos);
57 char *desc = znprintf(MAXDESCSIZE, "Ramdisk [%s]", &filename[10]);
58 boot_add_floppy(drive, desc, bootprio_find_named_rom(filename, 0));
62 ramdisk_copy(struct disk_op_s *op, int iswrite)
64 u32 offset = GET_GLOBALFLAT(op->drive_gf->cntl_id);
65 offset += (u32)op->lba * DISK_SECTOR_SIZE;
66 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
67 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
78 // Call int 1587 to copy data.
80 memset(&br, 0, sizeof(br));
85 br.cx = op->count * DISK_SECTOR_SIZE / 2;
86 call16_int(0x15, &br);
89 return DISK_RET_EBADTRACK;
90 return DISK_RET_SUCCESS;
94 process_ramdisk_op(struct disk_op_s *op)
96 if (!CONFIG_FLASH_FLOPPY)
99 switch (op->command) {
101 return ramdisk_copy(op, 0);
103 return ramdisk_copy(op, 1);
107 return DISK_RET_SUCCESS;
109 return DISK_RET_EPARAM;