Unify cd emulation access and main disk access code.
[seabios.git] / src / ramdisk.c
1 // 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 void
14 describe_ramdisk(int driveid)
15 {
16     printf("%s", Drives.drives[driveid].model);
17 }
18
19 void
20 ramdisk_setup()
21 {
22     if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
23         return;
24
25     // Find image.
26     struct cbfs_file *file = cbfs_findprefix("floppyimg/", NULL);
27     if (!file)
28         return;
29     u32 size = cbfs_datasize(file);
30     dprintf(3, "Found floppy file %s of size %d\n", cbfs_filename(file), size);
31     int ftype = find_floppy_type(size);
32     if (ftype < 0) {
33         dprintf(3, "No floppy type found for ramdisk size\n");
34         return;
35     }
36
37     // Allocate ram for image.
38     void *pos = memalign_tmphigh(PAGE_SIZE, size);
39     if (!pos) {
40         dprintf(3, "Not enough memory for ramdisk\n");
41         return;
42     }
43     add_e820((u32)pos, size, E820_RESERVED);
44
45     // Copy image into ram.
46     cbfs_copyfile(file, pos, size);
47
48     // Setup driver.
49     dprintf(1, "Mapping CBFS floppy %s to addr %p\n", cbfs_filename(file), pos);
50     int driveid = addFloppy((u32)pos, ftype, DTYPE_RAMDISK);
51     if (driveid >= 0)
52         strtcpy(Drives.drives[driveid].model, cbfs_filename(file)
53                 , ARRAY_SIZE(Drives.drives[driveid].model));
54 }
55
56 static int
57 ramdisk_copy(struct disk_op_s *op, int iswrite)
58 {
59     u32 offset = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
60     offset += (u32)op->lba * DISK_SECTOR_SIZE;
61     u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
62     u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
63
64     u64 gdt[6];
65     if (iswrite) {
66         gdt[2] = opd;
67         gdt[3] = ramd;
68     } else {
69         gdt[2] = ramd;
70         gdt[3] = opd;
71     }
72
73     // Call int 1587 to copy data.
74     struct bregs br;
75     memset(&br, 0, sizeof(br));
76     br.flags = F_CF;
77     br.ah = 0x87;
78     br.es = GET_SEG(SS);
79     br.si = (u32)gdt;
80     br.cx = op->count * DISK_SECTOR_SIZE / 2;
81     call16_int(0x15, &br);
82
83     if (br.flags & F_CF)
84         return DISK_RET_EBADTRACK;
85     return DISK_RET_SUCCESS;
86 }
87
88 int
89 process_ramdisk_op(struct disk_op_s *op)
90 {
91     if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
92         return 0;
93
94     switch (op->command) {
95     case CMD_READ:
96         return ramdisk_copy(op, 0);
97     case CMD_WRITE:
98         return ramdisk_copy(op, 1);
99     case CMD_VERIFY:
100     case CMD_FORMAT:
101     case CMD_RESET:
102         return DISK_RET_SUCCESS;
103     default:
104         op->count = 0;
105         return DISK_RET_EPARAM;
106     }
107 }