blob: adec1d1b355aef21098b0de89ad31c46c5e9b45d [file] [log] [blame]
Kevin O'Connora3855ad2009-08-16 21:59:40 -04001// Code for emulating a drive via high-memory accesses.
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
Kevin O'Connor1902c942013-10-26 11:48:06 -04007#include "biosvar.h" // GET_GLOBALFLAT
Kevin O'Connor135f3f62013-09-14 23:57:26 -04008#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "bregs.h" // struct bregs
Kevin O'Connorc167e542015-09-29 09:40:46 -040010#include "e820map.h" // e820_add
Kevin O'Connor3abdc7c2015-06-30 11:10:41 -040011#include "malloc.h" // memalign_tmphigh
Kevin O'Connorc167e542015-09-29 09:40:46 -040012#include "memmap.h" // PAGE_SIZE
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "output.h" // dprintf
Kevin O'Connor41639f82013-09-14 19:37:36 -040014#include "romfile.h" // romfile_findprefix
Kevin O'Connor3df600b2013-09-14 19:28:55 -040015#include "stacks.h" // call16_int
Kevin O'Connor135f3f62013-09-14 23:57:26 -040016#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040017#include "string.h" // memset
Kevin O'Connor135f3f62013-09-14 23:57:26 -040018#include "util.h" // process_ramdisk_op
Kevin O'Connora3855ad2009-08-16 21:59:40 -040019
Kevin O'Connora3855ad2009-08-16 21:59:40 -040020void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050021ramdisk_setup(void)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040022{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040023 if (!CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040024 return;
25
26 // Find image.
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040027 struct romfile_s *file = romfile_findprefix("floppyimg/", NULL);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040028 if (!file)
29 return;
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040030 const char *filename = file->name;
31 u32 size = file->size;
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050032 dprintf(3, "Found floppy file %s of size %d\n", filename, size);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040033 int ftype = find_floppy_type(size);
34 if (ftype < 0) {
35 dprintf(3, "No floppy type found for ramdisk size\n");
36 return;
37 }
38
39 // Allocate ram for image.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040040 void *pos = memalign_tmphigh(PAGE_SIZE, size);
41 if (!pos) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050042 warn_noalloc();
Kevin O'Connora3855ad2009-08-16 21:59:40 -040043 return;
44 }
Kevin O'Connorc167e542015-09-29 09:40:46 -040045 e820_add((u32)pos, size, E820_RESERVED);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040046
47 // Copy image into ram.
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040048 int ret = file->copy(file, pos, size);
49 if (ret < 0)
50 return;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040051
52 // Setup driver.
Kevin O'Connor1902c942013-10-26 11:48:06 -040053 struct drive_s *drive = init_floppy((u32)pos, ftype);
54 if (!drive)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050055 return;
Kevin O'Connor1902c942013-10-26 11:48:06 -040056 drive->type = DTYPE_RAMDISK;
Kevin O'Connor7dbadfc2015-07-23 09:19:23 -040057 dprintf(1, "Mapping floppy %s to addr %p\n", filename, pos);
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050058 char *desc = znprintf(MAXDESCSIZE, "Ramdisk [%s]", &filename[10]);
Kevin O'Connor1902c942013-10-26 11:48:06 -040059 boot_add_floppy(drive, desc, bootprio_find_named_rom(filename, 0));
Kevin O'Connora3855ad2009-08-16 21:59:40 -040060}
61
62static int
Kevin O'Connor907a41c2009-08-30 19:31:22 -040063ramdisk_copy(struct disk_op_s *op, int iswrite)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040064{
Kevin O'Connor1902c942013-10-26 11:48:06 -040065 u32 offset = GET_GLOBALFLAT(op->drive_gf->cntl_id);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040066 offset += (u32)op->lba * DISK_SECTOR_SIZE;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040067 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
68 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
69
70 u64 gdt[6];
71 if (iswrite) {
72 gdt[2] = opd;
73 gdt[3] = ramd;
74 } else {
75 gdt[2] = ramd;
76 gdt[3] = opd;
77 }
78
Kevin O'Connor907a41c2009-08-30 19:31:22 -040079 // Call int 1587 to copy data.
Kevin O'Connora3855ad2009-08-16 21:59:40 -040080 struct bregs br;
81 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040082 br.flags = F_CF|F_IF;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040083 br.ah = 0x87;
84 br.es = GET_SEG(SS);
85 br.si = (u32)gdt;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040086 br.cx = op->count * DISK_SECTOR_SIZE / 2;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040087 call16_int(0x15, &br);
88
Kevin O'Connor907a41c2009-08-30 19:31:22 -040089 if (br.flags & F_CF)
90 return DISK_RET_EBADTRACK;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040091 return DISK_RET_SUCCESS;
92}
93
94int
Kevin O'Connor17856452015-07-07 14:56:20 -040095ramdisk_process_op(struct disk_op_s *op)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040096{
Kevin O'Connor897fb112013-02-07 23:32:48 -050097 if (!CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040098 return 0;
99
100 switch (op->command) {
101 case CMD_READ:
Kevin O'Connor907a41c2009-08-30 19:31:22 -0400102 return ramdisk_copy(op, 0);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400103 case CMD_WRITE:
Kevin O'Connor907a41c2009-08-30 19:31:22 -0400104 return ramdisk_copy(op, 1);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400105 default:
Kevin O'Connor85c72c62015-07-07 09:01:52 -0400106 return default_process_op(op);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400107 }
108}