forked from Dniel97/segatools
nu/nvram.c: Add helper for NVRAM-class devices
This commit is contained in:
parent
c340258be6
commit
278108a914
@ -9,5 +9,7 @@ nu_lib = static_library(
|
||||
],
|
||||
sources : [
|
||||
'guid.c',
|
||||
'nvram.c',
|
||||
'nvram.h',
|
||||
],
|
||||
)
|
||||
|
80
nu/nvram.c
Normal file
80
nu/nvram.c
Normal file
@ -0,0 +1,80 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "util/dprintf.h"
|
||||
|
||||
#include "nu/nvram.h"
|
||||
|
||||
HRESULT nvram_open_file(HANDLE *out, wchar_t *path, size_t size)
|
||||
{
|
||||
LARGE_INTEGER cur_size;
|
||||
LARGE_INTEGER pos;
|
||||
HANDLE file;
|
||||
HRESULT hr;
|
||||
BOOL ok;
|
||||
|
||||
assert(out != NULL);
|
||||
assert(path != NULL);
|
||||
|
||||
*out = NULL;
|
||||
|
||||
file = CreateFileW(
|
||||
path,
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ,
|
||||
NULL,
|
||||
OPEN_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL,
|
||||
NULL);
|
||||
|
||||
if (file == INVALID_HANDLE_VALUE) {
|
||||
hr = HRESULT_FROM_WIN32(GetLastError());
|
||||
dprintf("%S: Error opening backing store: %x\n", path, (int) hr);
|
||||
|
||||
goto end;
|
||||
}
|
||||
|
||||
ok = GetFileSizeEx(file, &cur_size);
|
||||
|
||||
if (!ok) {
|
||||
hr = HRESULT_FROM_WIN32(GetLastError());
|
||||
dprintf("%S: GetFileSizeEx failed: %x\n", path, (int) hr);
|
||||
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cur_size.QuadPart != (uint64_t) size) {
|
||||
pos.QuadPart = (uint64_t) size;
|
||||
ok = SetFilePointerEx(file, pos, NULL, FILE_BEGIN);
|
||||
|
||||
if (!ok) {
|
||||
hr = HRESULT_FROM_WIN32(GetLastError());
|
||||
dprintf("%S: SetFilePointerEx failed: %x\n", path, (int) hr);
|
||||
|
||||
goto end;
|
||||
}
|
||||
|
||||
ok = SetEndOfFile(file);
|
||||
|
||||
if (!ok) {
|
||||
hr = HRESULT_FROM_WIN32(GetLastError());
|
||||
dprintf("%S: SetEndOfFile failed: %x\n", path, (int) hr);
|
||||
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
*out = file;
|
||||
file = INVALID_HANDLE_VALUE;
|
||||
|
||||
hr = S_OK;
|
||||
|
||||
end:
|
||||
if (file != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(file);
|
||||
}
|
||||
|
||||
return hr;
|
||||
}
|
7
nu/nvram.h
Normal file
7
nu/nvram.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
HRESULT nvram_open_file(HANDLE *out, wchar_t *path, size_t size);
|
Loading…
Reference in New Issue
Block a user