2020-02-20 04:41:53 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
2020-02-21 00:18:40 +01:00
|
|
|
#include <errno.h>
|
2020-05-07 03:57:01 +02:00
|
|
|
#include "umka.h"
|
2020-03-11 04:02:33 +01:00
|
|
|
#include "trace.h"
|
2020-05-07 03:41:08 +02:00
|
|
|
#include "vdisk.h"
|
2020-02-20 04:41:53 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FILE *file;
|
|
|
|
uint32_t sect_size;
|
2020-03-07 23:09:36 +01:00
|
|
|
uint64_t sect_cnt;
|
|
|
|
unsigned cache_size;
|
2020-05-07 03:41:08 +02:00
|
|
|
int adjust_cache_size;
|
2020-02-20 04:41:53 +01:00
|
|
|
} vdisk_t;
|
|
|
|
|
2020-05-07 03:41:08 +02:00
|
|
|
void *vdisk_init(const char *fname, int adjust_cache_size, size_t cache_size) {
|
2020-02-20 04:41:53 +01:00
|
|
|
FILE *f = fopen(fname, "r+");
|
2020-02-21 00:18:40 +01:00
|
|
|
if (!f) {
|
|
|
|
printf("vdisk: can't open file '%s': %s\n", fname, strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-20 04:41:53 +01:00
|
|
|
fseeko(f, 0, SEEK_END);
|
|
|
|
off_t fsize = ftello(f);
|
|
|
|
fseeko(f, 0, SEEK_SET);
|
|
|
|
size_t sect_size = 512;
|
|
|
|
if (strstr(fname, "s4096") != NULL || strstr(fname, "s4k") != NULL) {
|
|
|
|
sect_size = 4096;
|
|
|
|
}
|
|
|
|
vdisk_t *vdisk = (vdisk_t*)malloc(sizeof(vdisk_t));
|
2020-03-07 23:09:36 +01:00
|
|
|
*vdisk = (vdisk_t){.file = f,
|
|
|
|
.sect_size = sect_size,
|
|
|
|
.sect_cnt = (uint64_t)fsize / sect_size,
|
2020-05-07 03:41:08 +02:00
|
|
|
.cache_size = cache_size,
|
|
|
|
.adjust_cache_size = adjust_cache_size};
|
2020-02-20 04:41:53 +01:00
|
|
|
return vdisk;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__stdcall__))
|
|
|
|
void vdisk_close(void *userdata) {
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_OFF();
|
2020-02-20 04:41:53 +01:00
|
|
|
vdisk_t *vdisk = userdata;
|
|
|
|
fclose(vdisk->file);
|
|
|
|
free(vdisk);
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_ON();
|
2020-02-20 04:41:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__stdcall__))
|
2020-03-07 23:09:36 +01:00
|
|
|
int vdisk_read(void *userdata, void *buffer, off_t startsector,
|
|
|
|
size_t *numsectors) {
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_OFF();
|
2020-02-20 04:41:53 +01:00
|
|
|
vdisk_t *vdisk = userdata;
|
|
|
|
fseeko(vdisk->file, startsector * vdisk->sect_size, SEEK_SET);
|
|
|
|
fread(buffer, *numsectors * vdisk->sect_size, 1, vdisk->file);
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_ON();
|
2020-02-20 04:41:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__stdcall__))
|
2020-03-07 23:09:36 +01:00
|
|
|
int vdisk_write(void *userdata, void *buffer, off_t startsector,
|
|
|
|
size_t *numsectors) {
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_OFF();
|
2020-02-20 04:41:53 +01:00
|
|
|
vdisk_t *vdisk = userdata;
|
|
|
|
fseeko(vdisk->file, startsector * vdisk->sect_size, SEEK_SET);
|
|
|
|
fwrite(buffer, *numsectors * vdisk->sect_size, 1, vdisk->file);
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_ON();
|
2020-02-20 04:41:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((__stdcall__))
|
|
|
|
int vdisk_querymedia(void *userdata, diskmediainfo_t *minfo) {
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_OFF();
|
2020-02-20 04:41:53 +01:00
|
|
|
vdisk_t *vdisk = userdata;
|
|
|
|
minfo->flags = 0u;
|
|
|
|
minfo->sector_size = vdisk->sect_size;
|
|
|
|
minfo->capacity = vdisk->sect_cnt;
|
2020-03-11 04:02:33 +01:00
|
|
|
COVERAGE_ON();
|
2020-02-20 04:41:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2020-02-21 03:21:09 +01:00
|
|
|
|
|
|
|
__attribute__((__stdcall__))
|
2020-05-07 03:41:08 +02:00
|
|
|
size_t vdisk_adjust_cache_size(void *userdata, size_t suggested_size) {
|
|
|
|
vdisk_t *vdisk = userdata;
|
|
|
|
if (vdisk->adjust_cache_size) {
|
|
|
|
return vdisk->cache_size;
|
|
|
|
} else {
|
|
|
|
return suggested_size;
|
|
|
|
}
|
2020-02-21 03:21:09 +01:00
|
|
|
}
|
2020-05-07 03:41:08 +02:00
|
|
|
|
|
|
|
diskfunc_t vdisk_functions = {
|
|
|
|
.strucsize = sizeof(diskfunc_t),
|
|
|
|
.close = vdisk_close,
|
|
|
|
.closemedia = NULL,
|
|
|
|
.querymedia = vdisk_querymedia,
|
|
|
|
.read = vdisk_read,
|
|
|
|
.write = vdisk_write,
|
|
|
|
.flush = NULL,
|
|
|
|
.adjust_cache_size = vdisk_adjust_cache_size,
|
|
|
|
};
|