Skip to content

Instantly share code, notes, and snippets.

@bemxio
Last active May 24, 2024 12:23
Show Gist options
  • Save bemxio/47d1b6c62d005f4c12bd63b64159ff02 to your computer and use it in GitHub Desktop.
Save bemxio/47d1b6c62d005f4c12bd63b64159ff02 to your computer and use it in GitHub Desktop.
A simple script made in C for dumping the bootloader on Windows
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <https://unlicense.org>
*/
#include <windows.h>
#include <stdio.h>
#define SECTOR_SIZE 512
#define BOOTLOADER_PATH "bootloader.bin"
int main() {
HANDLE drive = CreateFile("\\\\.\\PhysicalDrive0", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
HANDLE file = CreateFile(BOOTLOADER_PATH, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (drive == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Error: couldn't open a handle to the drive.\n");
fprintf(stderr, "Error code: %d\n", GetLastError());
fprintf(stderr, "Make sure you're running this program as administrator.\n");
return 1;
}
char buffer[SECTOR_SIZE];
if (ReadFile(drive, buffer, SECTOR_SIZE, NULL, NULL)) {
printf("Success: read the boot sector the drive.\n");
} else {
fprintf(stderr, "Error: couldn't read the boot sector from the drive.\n");
fprintf(stderr, "Code: %d\n", GetLastError());
return 1;
}
if (WriteFile(file, buffer, SECTOR_SIZE, NULL, NULL)) {
printf("Success: wrote the boot sector to the file.\n");
} else {
fprintf(stderr, "Error: couldn't write the boot sector to the file.\n");
fprintf(stderr, "Code: %d\n", GetLastError());
return 1;
}
return CloseHandle(drive) && CloseHandle(file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment