Be sure to add "void" to all function prototypes that take no args.
[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(struct drive_s *drive_g)
15 {
16     printf("%s", drive_g->model);
17 }
18
19 void
20 ramdisk_setup(void)
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     struct drive_s *drive_g = addFloppy((u32)pos, ftype, DTYPE_RAMDISK);
51     if (drive_g)
52         strtcpy(drive_g->model, cbfs_filename(file), ARRAY_SIZE(drive_g->model));
53 }
54
55 static int
56 ramdisk_copy(struct disk_op_s *op, int iswrite)
57 {
58     u32 offset = GET_GLOBAL(op->drive_g->cntl_id);
59     offset += (u32)op->lba * DISK_SECTOR_SIZE;
60     u64 opd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE((u32)op->buf_fl);
61     u64 ramd = GDT_DATA | GDT_LIMIT(0xfffff) | GDT_BASE(offset);
62
63     u64 gdt[6];
64     if (iswrite) {
65         gdt[2] = opd;
66         gdt[3] = ramd;
67     } else {
68         gdt[2] = ramd;
69         gdt[3] = opd;
70     }
71
72     // Call int 1587 to copy data.
73     struct bregs br;
74     memset(&br, 0, sizeof(br));
75     br.flags = F_CF|F_IF;
76     br.ah = 0x87;
77     br.es = GET_SEG(SS);
78     br.si = (u32)gdt;
79     br.cx = op->count * DISK_SECTOR_SIZE / 2;
80     call16_int(0x15, &br);
81
82     if (br.flags & F_CF)
83         return DISK_RET_EBADTRACK;
84     return DISK_RET_SUCCESS;
85 }
86
87 int
88 process_ramdisk_op(struct disk_op_s *op)
89 {
90     if (!CONFIG_COREBOOT_FLASH || !CONFIG_FLASH_FLOPPY)
91         return 0;
92
93     switch (op->command) {
94     case CMD_READ:
95         return ramdisk_copy(op, 0);
96     case CMD_WRITE:
97         return ramdisk_copy(op, 1);
98     case CMD_VERIFY:
99     case CMD_FORMAT:
100     case CMD_RESET:
101         return DISK_RET_SUCCESS;
102     default:
103         op->count = 0;
104         return DISK_RET_EPARAM;
105     }
106 }