blob: bae30e213d419f25bb36aee6a938341512ef9ee5 [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
7#include "disk.h" // process_ramdisk_op
8#include "util.h" // dprintf
9#include "memmap.h" // add_e820
10#include "biosvar.h" // GET_GLOBAL
11#include "bregs.h" // struct bregs
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050012#include "boot.h" // boot_add_floppy
Kevin O'Connora3855ad2009-08-16 21:59:40 -040013
Kevin O'Connora3855ad2009-08-16 21:59:40 -040014void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050015ramdisk_setup(void)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040016{
Kevin O'Connord9c93612010-03-20 11:00:45 -040017 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040018 return;
19
20 // Find image.
Kevin O'Connor00882592009-08-18 22:21:10 -040021 struct cbfs_file *file = cbfs_findprefix("floppyimg/", NULL);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040022 if (!file)
23 return;
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050024 const char *filename = cbfs_filename(file);
Kevin O'Connor00882592009-08-18 22:21:10 -040025 u32 size = cbfs_datasize(file);
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050026 dprintf(3, "Found floppy file %s of size %d\n", filename, size);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040027 int ftype = find_floppy_type(size);
28 if (ftype < 0) {
29 dprintf(3, "No floppy type found for ramdisk size\n");
30 return;
31 }
32
33 // Allocate ram for image.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040034 void *pos = memalign_tmphigh(PAGE_SIZE, size);
35 if (!pos) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050036 warn_noalloc();
Kevin O'Connora3855ad2009-08-16 21:59:40 -040037 return;
38 }
Kevin O'Connor907a41c2009-08-30 19:31:22 -040039 add_e820((u32)pos, size, E820_RESERVED);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040040
41 // Copy image into ram.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040042 cbfs_copyfile(file, pos, size);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040043
44 // Setup driver.
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050045 struct drive_s *drive_g = init_floppy((u32)pos, ftype);
Kevin O'Connor575ffc82010-02-21 23:20:10 -050046 if (!drive_g)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050047 return;
48 drive_g->type = DTYPE_RAMDISK;
Kevin O'Connorc4bd3b92010-12-29 21:59:44 -050049 dprintf(1, "Mapping CBFS floppy %s to addr %p\n", filename, pos);
50 char *desc = znprintf(MAXDESCSIZE, "Ramdisk [%s]", &filename[10]);
51 boot_add_floppy(drive_g, desc, bootprio_find_named_rom(filename, 0));
Kevin O'Connora3855ad2009-08-16 21:59:40 -040052}
53
54static int
Kevin O'Connor907a41c2009-08-30 19:31:22 -040055ramdisk_copy(struct disk_op_s *op, int iswrite)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040056{
Kevin O'Connor77d227b2009-10-22 21:48:39 -040057 u32 offset = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040058 offset += (u32)op->lba * DISK_SECTOR_SIZE;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040059 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
60 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
61
62 u64 gdt[6];
63 if (iswrite) {
64 gdt[2] = opd;
65 gdt[3] = ramd;
66 } else {
67 gdt[2] = ramd;
68 gdt[3] = opd;
69 }
70
Kevin O'Connor907a41c2009-08-30 19:31:22 -040071 // Call int 1587 to copy data.
Kevin O'Connora3855ad2009-08-16 21:59:40 -040072 struct bregs br;
73 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040074 br.flags = F_CF|F_IF;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040075 br.ah = 0x87;
76 br.es = GET_SEG(SS);
77 br.si = (u32)gdt;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040078 br.cx = op->count * DISK_SECTOR_SIZE / 2;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040079 call16_int(0x15, &br);
80
Kevin O'Connor907a41c2009-08-30 19:31:22 -040081 if (br.flags & F_CF)
82 return DISK_RET_EBADTRACK;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040083 return DISK_RET_SUCCESS;
84}
85
86int
87process_ramdisk_op(struct disk_op_s *op)
88{
Kevin O'Connord9c93612010-03-20 11:00:45 -040089 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040090 return 0;
91
92 switch (op->command) {
93 case CMD_READ:
Kevin O'Connor907a41c2009-08-30 19:31:22 -040094 return ramdisk_copy(op, 0);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040095 case CMD_WRITE:
Kevin O'Connor907a41c2009-08-30 19:31:22 -040096 return ramdisk_copy(op, 1);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040097 case CMD_VERIFY:
98 case CMD_FORMAT:
99 case CMD_RESET:
100 return DISK_RET_SUCCESS;
101 default:
102 op->count = 0;
103 return DISK_RET_EPARAM;
104 }
105}