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