forked from arnavbhatt288/installfreeldr-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
volume.c
91 lines (70 loc) · 1.8 KB
/
volume.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* PROJECT: ReactOS FreeLoader installer for Linux
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: Volume functions
* COPYRIGHT: Copyright 2001 Brian Palmer ([email protected])
* Copyright 2019 Arnav Bhatt ([email protected])
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "volume.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
static int hDiskVolume = 0;
bool OpenVolume(char* lpszVolumeName)
{
char RealVolumeName[512];
strcpy(RealVolumeName, lpszVolumeName);
printf("Opening volume %s\n", lpszVolumeName);
hDiskVolume = open(lpszVolumeName, O_RDWR | O_SYNC);
if (hDiskVolume < 0)
{
perror("OpenVolume() failed!");
return false;
}
return true;
}
void CloseVolume(void)
{
close(hDiskVolume);
}
bool ReadVolumeSector(long SectorNumber, void* SectorBuffer)
{
int dwNumberOfBytesRead;
int dwFilePosition;
dwFilePosition = lseek(hDiskVolume, (SectorNumber* 512), SEEK_SET);
if (dwFilePosition != (SectorNumber * 512))
{
perror("ReadVolumeSector() failed!");
return false;
}
dwNumberOfBytesRead = read(hDiskVolume, SectorBuffer, 512);
if (dwNumberOfBytesRead != 512)
{
perror("ReadVolumeSector() failed!");
return false;
}
return true;
}
bool WriteVolumeSector(long SectorNumber, void* SectorBuffer)
{
int dwNumberOfBytesWritten;
int dwFilePosition;
dwFilePosition = lseek(hDiskVolume, (SectorNumber * 512), SEEK_SET);
if (dwFilePosition != (SectorNumber * 512))
{
perror("WriteVolumeSector() failed!");
return false;
}
dwNumberOfBytesWritten = write(hDiskVolume, SectorBuffer, 512);
if (dwNumberOfBytesWritten != 512)
{
perror("WriteVolumeSector() failed!");
return false;
}
return true;
}