blob: be1de56c2152d9486e69c5d248d00bbed932ac1e [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
12
Kevin O'Connora3855ad2009-08-16 21:59:40 -040013void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050014ramdisk_setup(void)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040015{
Kevin O'Connord9c93612010-03-20 11:00:45 -040016 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040017 return;
18
19 // Find image.
Kevin O'Connor00882592009-08-18 22:21:10 -040020 struct cbfs_file *file = cbfs_findprefix("floppyimg/", NULL);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040021 if (!file)
22 return;
Kevin O'Connor00882592009-08-18 22:21:10 -040023 u32 size = cbfs_datasize(file);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040024 dprintf(3, "Found floppy file %s of size %d\n", cbfs_filename(file), size);
25 int ftype = find_floppy_type(size);
26 if (ftype < 0) {
27 dprintf(3, "No floppy type found for ramdisk size\n");
28 return;
29 }
30
31 // Allocate ram for image.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040032 void *pos = memalign_tmphigh(PAGE_SIZE, size);
33 if (!pos) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050034 warn_noalloc();
Kevin O'Connora3855ad2009-08-16 21:59:40 -040035 return;
36 }
Kevin O'Connor907a41c2009-08-30 19:31:22 -040037 add_e820((u32)pos, size, E820_RESERVED);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040038
39 // Copy image into ram.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040040 cbfs_copyfile(file, pos, size);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040041
42 // Setup driver.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040043 dprintf(1, "Mapping CBFS floppy %s to addr %p\n", cbfs_filename(file), pos);
Kevin O'Connor77d227b2009-10-22 21:48:39 -040044 struct drive_s *drive_g = addFloppy((u32)pos, ftype, DTYPE_RAMDISK);
Kevin O'Connor575ffc82010-02-21 23:20:10 -050045 if (!drive_g)
46 strtcpy(drive_g->desc, cbfs_filename(file), MAXDESCSIZE);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040047}
48
49static int
Kevin O'Connor907a41c2009-08-30 19:31:22 -040050ramdisk_copy(struct disk_op_s *op, int iswrite)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040051{
Kevin O'Connor77d227b2009-10-22 21:48:39 -040052 u32 offset = GET_GLOBAL(op->drive_g->cntl_id);
Kevin O'Connor36c93a52009-09-12 19:35:04 -040053 offset += (u32)op->lba * DISK_SECTOR_SIZE;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040054 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
55 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
56
57 u64 gdt[6];
58 if (iswrite) {
59 gdt[2] = opd;
60 gdt[3] = ramd;
61 } else {
62 gdt[2] = ramd;
63 gdt[3] = opd;
64 }
65
Kevin O'Connor907a41c2009-08-30 19:31:22 -040066 // Call int 1587 to copy data.
Kevin O'Connora3855ad2009-08-16 21:59:40 -040067 struct bregs br;
68 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -040069 br.flags = F_CF|F_IF;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040070 br.ah = 0x87;
71 br.es = GET_SEG(SS);
72 br.si = (u32)gdt;
Kevin O'Connor36c93a52009-09-12 19:35:04 -040073 br.cx = op->count * DISK_SECTOR_SIZE / 2;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040074 call16_int(0x15, &br);
75
Kevin O'Connor907a41c2009-08-30 19:31:22 -040076 if (br.flags & F_CF)
77 return DISK_RET_EBADTRACK;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040078 return DISK_RET_SUCCESS;
79}
80
81int
82process_ramdisk_op(struct disk_op_s *op)
83{
Kevin O'Connord9c93612010-03-20 11:00:45 -040084 if (!CONFIG_COREBOOT || !CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040085 return 0;
86
87 switch (op->command) {
88 case CMD_READ:
Kevin O'Connor907a41c2009-08-30 19:31:22 -040089 return ramdisk_copy(op, 0);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040090 case CMD_WRITE:
Kevin O'Connor907a41c2009-08-30 19:31:22 -040091 return ramdisk_copy(op, 1);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040092 case CMD_VERIFY:
93 case CMD_FORMAT:
94 case CMD_RESET:
95 return DISK_RET_SUCCESS;
96 default:
97 op->count = 0;
98 return DISK_RET_EPARAM;
99 }
100}