First test to read and edit the firmware, added defines for the required offsets etc

This commit is contained in:
2022-02-19 20:09:11 +01:00
parent 873aaa670b
commit c161f29aa1
6 changed files with 129 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include "../include/change_key.h"
void set_key_value(char* firmware_buffer, int key, int value) {
memset(firmware_buffer + key, value, 1);
}
+49
View File
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include "../include/firmware_handling.h"
fbuffer_t* get_firmware_buffer(char* filename) {
fbuffer_t buffer;
fbuffer_t* p_fb = &buffer;
FILE *firmware = fopen(filename, "rb");
if(!firmware) {
perror("Error while reading in Firmware File.\r\nExiting.");
exit(EXIT_FAILURE);
}
struct stat sb;
if(stat(filename, &sb) == -1) {
perror("Error while reading Firmware metadata.\r\nExiting.");
exit(EXIT_FAILURE);
}
p_fb->size = sb.st_size;
p_fb->buffer = malloc(p_fb->size);
fread(p_fb->buffer, p_fb->size, 1, firmware);
/* for testing if the buffer could be read correclty */
// printf("buffer size: %d", p_fb->size);
fclose(firmware);
return p_fb;
}
void write_firmware_buffer(char* filename, fbuffer_t* p_fb) {
FILE *firmware_file = fopen(filename, "wb+");
if(!firmware_file) {
perror("Error while opening in Firmware File.\r\nExiting.");
exit(EXIT_FAILURE);
}
fwrite(p_fb->buffer, p_fb->size, 1, firmware_file);
fclose(firmware_file);
}
+27
View File
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "../include/change_key.h"
#include "../include/firmware_handling.h"
int main (int argc, char *argv[])
{
fbuffer_t* p_fb = get_firmware_buffer(argv[1]);
set_key_value(p_fb->buffer, KEY8, 0x1E);
/* for debugging -> print content of buffer to terminal */
/*
for(int i = 0; i<fb->size; i++){
putc(isprint(fb->buffer[i]) ? fb->buffer[i] : '.', stdout);
}
*/
char str[50];
sprintf(str, "new_%s", argv[1]);
write_firmware_buffer(str, p_fb);
return 0;
}