01648402b0e159e4f8cb7d0461e608092a49d5b4
[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 ramdisk_setup(void)
15 {
16     if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
17         return;
18
19     // Find image.
20     struct cbfs_file *file = cbfs_findprefix("floppyimg/", NULL);
21     if (!file)
22         return;
23     u32 size = cbfs_datasize(file);
24     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.
32     void *pos = memalign_tmphigh(PAGE_SIZE, size);
33     if (!pos) {
34         warn_noalloc();
35         return;
36     }
37     add_e820((u32)pos, size, E820_RESERVED);
38
39     // Copy image into ram.
40     cbfs_copyfile(file, pos, size);
41
42     // Setup driver.
43     dprintf(1, "Mapping CBFS floppy %s to addr %p\n", cbfs_filename(file), pos);
44     struct drive_s *drive_g = addFloppy((u32)pos, ftype, DTYPE_RAMDISK);
45     if (!drive_g)
46         strtcpy(drive_g->desc, cbfs_filename(file), MAXDESCSIZE);
47 }
48
49 static int
50 ramdisk_copy(struct disk_op_s *op, int iswrite)
51 {
52     u32 offset = GET_GLOBAL(op->drive_g->cntl_id);
53     offset += (u32)op->lba * DISK_SECTOR_SIZE;
54     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
66     // Call int 1587 to copy data.
67     struct bregs br;
68     memset(&br, 0, sizeof(br));
69     br.flags = F_CF|F_IF;
70     br.ah = 0x87;
71     br.es = GET_SEG(SS);
72     br.si = (u32)gdt;
73     br.cx = op->count * DISK_SECTOR_SIZE / 2;
74     call16_int(0x15, &br);
75
76     if (br.flags & F_CF)
77         return DISK_RET_EBADTRACK;
78     return DISK_RET_SUCCESS;
79 }
80
81 int
82 process_ramdisk_op(struct disk_op_s *op)
83 {
84     if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
85         return 0;
86
87     switch (op->command) {
88     case CMD_READ:
89         return ramdisk_copy(op, 0);
90     case CMD_WRITE:
91         return ramdisk_copy(op, 1);
92     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 }