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