/* * Block driver in RAM * * Copyright (c) 2007 Jim Boown * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "vl.h" #include "block_int.h" #include #ifndef QEMU_TOOL #include "exec-all.h" #endif typedef struct BDRVRamState { char * ram_data; } BDRVRamState; static int ram_probe(const uint8_t *buf, int buf_size, const char *filename) { if (strstart(filename, "ram:", NULL)) return 100; return 0; } static int ram_open(BlockDriverState *bs, const char *filename, int flags) { BDRVRamState *s = bs->opaque; if (!strstart(filename, "ram:", NULL)) return -1; int sectnum = atoi(filename+4); if (sectnum == 0) return -2; s->ram_data = qemu_mallocz(sectnum*512); if (s->ram_data == NULL) return -2; memset(s->ram_data, ' ', sectnum*512); bs->total_sectors = sectnum; return 0; } static int ram_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors) { BDRVRamState *s = bs->opaque; if (sector_num+nb_sectors > bs->total_sectors) return -1; memcpy(buf, &(s->ram_data[sector_num*512]), nb_sectors*512); return 0; } static int ram_write(BlockDriverState *bs, int64_t sector_num, const uint8_t *buf, int nb_sectors) { BDRVRamState *s = bs->opaque; if (sector_num+nb_sectors > bs->total_sectors) return -1; memcpy(&(s->ram_data[sector_num*512]), buf, nb_sectors*512); return 0; } static void ram_close(BlockDriverState *bs) { BDRVRamState *s = bs->opaque; qemu_free(s->ram_data); } static int ram_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int* n) { *n = bs->total_sectors - sector_num; if (*n > nb_sectors) *n = nb_sectors; else if (*n < 0) return 0; return 1; } BlockDriver bdrv_ram = { "ram", sizeof(BDRVRamState), ram_probe, ram_open, ram_read, ram_write, ram_close, NULL, NULL, ram_is_allocated, .protocol_name = "ram", };