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