blob: c6672a71ac1ef59b0a2c53a286fe4ecce4413d44 [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
13#define RAMDISK_SECTOR_SIZE 512
14
15void
Kevin O'Connor51fd0a12009-09-12 13:20:14 -040016describe_ramdisk(int driveid)
17{
18 printf("%s", Drives.drives[driveid].model);
19}
20
21void
Kevin O'Connora3855ad2009-08-16 21:59:40 -040022ramdisk_setup()
23{
24 if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
25 return;
26
27 // Find image.
Kevin O'Connor00882592009-08-18 22:21:10 -040028 struct cbfs_file *file = cbfs_findprefix("floppyimg/", NULL);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040029 if (!file)
30 return;
Kevin O'Connor00882592009-08-18 22:21:10 -040031 u32 size = cbfs_datasize(file);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040032 dprintf(3, "Found floppy file %s of size %d\n", cbfs_filename(file), size);
33 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) {
42 dprintf(3, "Not enough memory for ramdisk\n");
Kevin O'Connora3855ad2009-08-16 21:59:40 -040043 return;
44 }
Kevin O'Connor907a41c2009-08-30 19:31:22 -040045 add_e820((u32)pos, size, E820_RESERVED);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040046
47 // Copy image into ram.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040048 cbfs_copyfile(file, pos, size);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040049
50 // Setup driver.
Kevin O'Connor907a41c2009-08-30 19:31:22 -040051 dprintf(1, "Mapping CBFS floppy %s to addr %p\n", cbfs_filename(file), pos);
Kevin O'Connor51fd0a12009-09-12 13:20:14 -040052 int driveid = addFloppy((u32)pos, ftype, DTYPE_RAMDISK);
53 if (driveid >= 0)
54 strtcpy(Drives.drives[driveid].model, cbfs_filename(file)
55 , ARRAY_SIZE(Drives.drives[driveid].model));
Kevin O'Connora3855ad2009-08-16 21:59:40 -040056}
57
58static int
Kevin O'Connor907a41c2009-08-30 19:31:22 -040059ramdisk_copy(struct disk_op_s *op, int iswrite)
Kevin O'Connora3855ad2009-08-16 21:59:40 -040060{
61 u32 offset = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
62 offset += (u32)op->lba * RAMDISK_SECTOR_SIZE;
63 u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
64 u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
65
66 u64 gdt[6];
67 if (iswrite) {
68 gdt[2] = opd;
69 gdt[3] = ramd;
70 } else {
71 gdt[2] = ramd;
72 gdt[3] = opd;
73 }
74
Kevin O'Connor907a41c2009-08-30 19:31:22 -040075 // Call int 1587 to copy data.
Kevin O'Connora3855ad2009-08-16 21:59:40 -040076 struct bregs br;
77 memset(&br, 0, sizeof(br));
Kevin O'Connor907a41c2009-08-30 19:31:22 -040078 br.flags = F_CF;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040079 br.ah = 0x87;
80 br.es = GET_SEG(SS);
81 br.si = (u32)gdt;
82 br.cx = op->count * RAMDISK_SECTOR_SIZE / 2;
83 call16_int(0x15, &br);
84
Kevin O'Connor907a41c2009-08-30 19:31:22 -040085 if (br.flags & F_CF)
86 return DISK_RET_EBADTRACK;
Kevin O'Connora3855ad2009-08-16 21:59:40 -040087 return DISK_RET_SUCCESS;
88}
89
90int
91process_ramdisk_op(struct disk_op_s *op)
92{
93 if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
94 return 0;
95
96 switch (op->command) {
97 case CMD_READ:
Kevin O'Connor907a41c2009-08-30 19:31:22 -040098 return ramdisk_copy(op, 0);
Kevin O'Connora3855ad2009-08-16 21:59:40 -040099 case CMD_WRITE:
Kevin O'Connor907a41c2009-08-30 19:31:22 -0400100 return ramdisk_copy(op, 1);
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400101 case CMD_VERIFY:
102 case CMD_FORMAT:
103 case CMD_RESET:
104 return DISK_RET_SUCCESS;
105 default:
106 op->count = 0;
107 return DISK_RET_EPARAM;
108 }
109}