blob: eeddf2562892c7c3fa4202f4b8b1d334ab1bad14 [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'Connora3855ad2009-08-16 21:59:40 -04007#include "biosvar.h" // GET_GLOBAL
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'Connor9dea5902013-09-14 20:23:54 -040010#include "malloc.h" // malloc_fseg
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "memmap.h" // add_e820
12#include "output.h" // dprintf
Kevin O'Connor41639f82013-09-14 19:37:36 -040013#include "romfile.h" // romfile_findprefix
Kevin O'Connor3df600b2013-09-14 19:28:55 -040014#include "stacks.h" // call16_int
Kevin O'Connor135f3f62013-09-14 23:57:26 -040015#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040016#include "string.h" // memset
Kevin O'Connor135f3f62013-09-14 23:57:26 -040017#include "util.h" // process_ramdisk_op
Kevin O'Connora3855ad2009-08-16 21:59:40 -040018
Kevin O'Connora3855ad2009-08-16 21:59:40 -040019void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050020ramdisk_setup(void)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040021{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040022 if (!CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040023 return;
24
25 // Find image.
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040026 struct romfile_s *file = romfile_findprefix("floppyimg/", NULL);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040027 if (!file)
28 return;
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040029 const char *filename = file->name;
30 u32 size = file->size;
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050031 dprintf(3, "Found floppy file %s of size %d\n", filename, size);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040032 int ftype = find_floppy_type(size);
33 if (ftype < 0) {
34 dprintf(3, "No floppy type found for ramdisk size\n");
35 return;
36 }
37
38 // Allocate ram for image.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040039 void *pos = memalign_tmphigh(PAGE_SIZE, size);
40 if (!pos) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050041 warn_noalloc();
Kevin O'Connora3855ad2009-08-16 21:59:40 -040042 return;
43 }
Kevin O'Connor907a41c2009-08-30 19:31:22 -040044 add_e820((u32)pos, size, E820_RESERVED);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040045
46 // Copy image into ram.
Kevin O'Connor59d6ca52012-05-31 00:20:55 -040047 int ret = file->copy(file, pos, size);
48 if (ret < 0)
49 return;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040050
51 // Setup driver.
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050052 struct drive_s *drive_g = init_floppy((u32)pos, ftype);
Kevin O'Connor575ffc82010-02-21 23:20:10 -050053 if (!drive_g)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050054 return;
55 drive_g->type = DTYPE_RAMDISK;
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050056 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_g, desc, bootprio_find_named_rom(filename, 0));
Kevin O'Connora3855ad2009-08-16 21:59:40 -040059}
60
61static int
Kevin O'Connor907a41c2009-08-30 19:31:22 -040062ramdisk_copy(struct disk_op_s *op, int iswrite)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040063{
Kevin O'Connor77d227b2009-10-22 21:48:39 -040064 u32 offset = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040065 offset += (u32)op->lba * DISK_SECTOR_SIZE;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040066 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
67 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
68
69 u64 gdt[6];
70 if (iswrite) {
71 gdt[2] = opd;
72 gdt[3] = ramd;
73 } else {
74 gdt[2] = ramd;
75 gdt[3] = opd;
76 }
77
Kevin O'Connor907a41c2009-08-30 19:31:22 -040078 // Call int 1587 to copy data.
Kevin O'Connora3855ad2009-08-16 21:59:40 -040079 struct bregs br;
80 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040081 br.flags = F_CF|F_IF;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040082 br.ah = 0x87;
83 br.es = GET_SEG(SS);
84 br.si = (u32)gdt;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040085 br.cx = op->count * DISK_SECTOR_SIZE / 2;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040086 call16_int(0x15, &br);
87
Kevin O'Connor907a41c2009-08-30 19:31:22 -040088 if (br.flags & F_CF)
89 return DISK_RET_EBADTRACK;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040090 return DISK_RET_SUCCESS;
91}
92
93int
94process_ramdisk_op(struct disk_op_s *op)
95{
Kevin O'Connor897fb112013-02-07 23:32:48 -050096 if (!CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040097 return 0;
98
99 switch (op->command) {
100 case CMD_READ:
Kevin O'Connor907a41c2009-08-30 19:31:22 -0400101 return ramdisk_copy(op, 0);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400102 case CMD_WRITE:
Kevin O'Connor907a41c2009-08-30 19:31:22 -0400103 return ramdisk_copy(op, 1);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400104 case CMD_VERIFY:
105 case CMD_FORMAT:
106 case CMD_RESET:
107 return DISK_RET_SUCCESS;
108 default:
109 op->count = 0;
110 return DISK_RET_EPARAM;
111 }
112}