|
| 1 | +// Copyright (c) 2017 OhGodACompany - OhGodAGirl & OhGodAPet |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdint.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +#define ATOM_CSUM_OFFSET 0x21 |
| 8 | + |
| 9 | +int main(int argc, char **argv) |
| 10 | +{ |
| 11 | + FILE *VBIOSFile; |
| 12 | + uint8_t *VBIOS, CSum = 0; |
| 13 | + size_t VBIOSSize, BytesRead; |
| 14 | + |
| 15 | + if(argc != 2) |
| 16 | + { |
| 17 | + printf("Usage: %s <VBIOS file>\n", argv[0]); |
| 18 | + return(1); |
| 19 | + } |
| 20 | + |
| 21 | + VBIOSFile = fopen(argv[1], "rb+"); |
| 22 | + |
| 23 | + if(!VBIOSFile) |
| 24 | + { |
| 25 | + printf("Cannot open VBIOS image %s (does it exist?)\n", argv[1]); |
| 26 | + return(1); |
| 27 | + } |
| 28 | + |
| 29 | + fseek(VBIOSFile, 0UL, SEEK_END); |
| 30 | + VBIOSSize = ftell(VBIOSFile); |
| 31 | + rewind(VBIOSFile); |
| 32 | + |
| 33 | + VBIOS = (uint8_t *)malloc(sizeof(uint8_t) * VBIOSSize); |
| 34 | + |
| 35 | + BytesRead = fread(VBIOS, sizeof(uint8_t), VBIOSSize, VBIOSFile); |
| 36 | + |
| 37 | + if(BytesRead != VBIOSSize) |
| 38 | + { |
| 39 | + printf("Could not read entire VBIOS image.\n"); |
| 40 | + fclose(VBIOSFile); |
| 41 | + free(VBIOS); |
| 42 | + return(1); |
| 43 | + } |
| 44 | + |
| 45 | + for(int i = 0; i < (VBIOS[0x02] * 512); ++i) CSum += VBIOS[i]; |
| 46 | + |
| 47 | + if(!CSum) |
| 48 | + { |
| 49 | + printf("Checksum already valid (0x%02X), nothing to fix.\n", VBIOS[ATOM_CSUM_OFFSET]); |
| 50 | + fclose(VBIOSFile); |
| 51 | + free(VBIOS); |
| 52 | + return(0); |
| 53 | + } |
| 54 | + |
| 55 | + printf("Checksum invalid, fixing...\n"); |
| 56 | + |
| 57 | + VBIOS[ATOM_CSUM_OFFSET] -= CSum; |
| 58 | + |
| 59 | + fseek(VBIOSFile, ATOM_CSUM_OFFSET, SEEK_SET); |
| 60 | + |
| 61 | + if(fwrite(VBIOS + ATOM_CSUM_OFFSET, sizeof(uint8_t), 1, VBIOSFile) != 1) |
| 62 | + { |
| 63 | + printf("Error writing to VBIOS image.\n"); |
| 64 | + fclose(VBIOSFile); |
| 65 | + free(VBIOS); |
| 66 | + return(1); |
| 67 | + } |
| 68 | + |
| 69 | + fclose(VBIOSFile); |
| 70 | + free(VBIOS); |
| 71 | + return(0); |
| 72 | +} |
0 commit comments