1
0
Fork 0
forked from len0rd/rockbox

mkboot500: fix compilation on MacOS

code changes limited to L-31-37,
rest is trailing whitespace cleanup

Change-Id: Iaec8f1daec5112715085f1a59b0347044fcbbc09
This commit is contained in:
Christian Soffke 2024-12-17 13:33:35 +01:00
parent cede12f197
commit aeb4b6c34a

View file

@ -6,8 +6,8 @@
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/ * \/ \/ \/ \/ \/
* *
* Copyright (C) 2009 by Karl Kurbjun * Copyright (C) 2009 by Karl Kurbjun
* based on work by Shirour: * based on work by Shirour:
* http://www.mrobe.org/forum/viewtopic.php?f=6&t=2176 * http://www.mrobe.org/forum/viewtopic.php?f=6&t=2176
* $Id$ * $Id$
* *
@ -28,6 +28,13 @@
#include <inttypes.h> #include <inttypes.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "mr500.h" #include "mr500.h"
#ifdef __APPLE__
#include <libkern/OSByteOrder.h>
#define htole32(x) OSSwapHostToLittleInt32(x)
#define htole16(x) OSSwapHostToLittleInt16(x)
#define le32toh(x) OSSwapLittleToHostInt32(x)
#define le16toh(x) OSSwapLittleToHostInt16(x)
#endif
/* Notes about firmware: /* Notes about firmware:
* These notes are based on the work and observations of Shirour on the M:Robe * These notes are based on the work and observations of Shirour on the M:Robe
@ -54,7 +61,7 @@ int encrypt_array[16];
/* mr500_patch_file: This function modifies the specified file with the patches /* mr500_patch_file: This function modifies the specified file with the patches
* struct. * struct.
* *
* Parameters: * Parameters:
* filename: text filename * filename: text filename
* patches: pointer to structure array of patches * patches: pointer to structure array of patches
@ -68,44 +75,44 @@ int mr500_patch_file(char *filename, struct patch_single *patches,
int fdo; int fdo;
int ret=0; int ret=0;
uint32_t endian_int; uint32_t endian_int;
/* Open the file write only. */ /* Open the file write only. */
fdo = open(filename, O_WRONLY); fdo = open(filename, O_WRONLY);
if(fdo<0) { if(fdo<0) {
ret=-1; ret=-1;
} }
while(num_patches--) { while(num_patches--) {
/* seek to patch offset */ /* seek to patch offset */
if(lseek(fdo, patches[num_patches].offset, SEEK_SET) if(lseek(fdo, patches[num_patches].offset, SEEK_SET)
!= patches[num_patches].offset) { != patches[num_patches].offset) {
ret=-1; ret=-1;
break; break;
} }
/* Make sure patch is written in little endian format */ /* Make sure patch is written in little endian format */
endian_int = htole32(patches[num_patches].value); endian_int = htole32(patches[num_patches].value);
/* Write the patch value to the file */ /* Write the patch value to the file */
if(write(fdo, (void *) &endian_int, sizeof(endian_int)) < 0) { if(write(fdo, (void *) &endian_int, sizeof(endian_int)) < 0) {
ret = -1; ret = -1;
break; break;
} }
} }
/* Close the file and check for errors */ /* Close the file and check for errors */
if(close (fdo) < 0) { if(close (fdo) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_save_header: This function saves the Olympus header to the firmware /* mr500_save_header: This function saves the Olympus header to the firmware
* image. The values stored in the header are explained above. Note that this * image. The values stored in the header are explained above. Note that this
* will truncate a file. The header is stored in little endian format. * will truncate a file. The header is stored in little endian format.
* *
* Parameters: * Parameters:
* filename: text filename * filename: text filename
* header: pointer to header structure to be saved * header: pointer to header structure to be saved
@ -116,22 +123,22 @@ int mr500_patch_file(char *filename, struct patch_single *patches,
int mr500_save_header(char *filename, struct olympus_header *header) { int mr500_save_header(char *filename, struct olympus_header *header) {
int fdo; int fdo;
int ret=0; int ret=0;
/* Temporary header used for storing the header in little endian. */ /* Temporary header used for storing the header in little endian. */
struct olympus_header save; struct olympus_header save;
/* Open the file write only and truncate it. If it doesn't exist create. */ /* Open the file write only and truncate it. If it doesn't exist create. */
fdo = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); fdo = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if(fdo<0) { if(fdo<0) {
ret=-1; ret=-1;
} }
/* Header is stored at offset 0 (Not really needed) */ /* Header is stored at offset 0 (Not really needed) */
if(lseek(fdo, 0, SEEK_SET) != 0) { if(lseek(fdo, 0, SEEK_SET) != 0) {
ret=-1; ret=-1;
} }
/* Convert header to Little Endian */ /* Convert header to Little Endian */
memcpy(&save.magic_name, &header->magic_name, 8*sizeof(int8_t)); memcpy(&save.magic_name, &header->magic_name, 8*sizeof(int8_t));
save.unknown = htole32(header->unknown); save.unknown = htole32(header->unknown);
@ -144,19 +151,19 @@ int mr500_save_header(char *filename, struct olympus_header *header) {
if(write(fdo, (void *) &save, sizeof(save)) < 0) { if(write(fdo, (void *) &save, sizeof(save)) < 0) {
ret = -1; ret = -1;
} }
/* Close the file and check for errors */ /* Close the file and check for errors */
if(close (fdo) < 0) { if(close (fdo) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_read_header: This function reads the Olympus header and converts it to /* mr500_read_header: This function reads the Olympus header and converts it to
* the host endian format. The values stored in the header are explained above. * the host endian format. The values stored in the header are explained above.
* The header is stored in little endian format. * The header is stored in little endian format.
* *
* Parameters: * Parameters:
* filename: text filename * filename: text filename
* header: pointer to header structure to store header read from file * header: pointer to header structure to store header read from file
@ -167,43 +174,43 @@ int mr500_save_header(char *filename, struct olympus_header *header) {
int mr500_read_header(char *filename, struct olympus_header *header) { int mr500_read_header(char *filename, struct olympus_header *header) {
int fdi; int fdi;
int ret = 0; int ret = 0;
/* Open the file read only */ /* Open the file read only */
fdi = open(filename, O_RDONLY); fdi = open(filename, O_RDONLY);
if(fdi<0) { if(fdi<0) {
ret=-1; ret=-1;
} }
/* Header is stored at offset 0 (Not really needed) */ /* Header is stored at offset 0 (Not really needed) */
if(lseek(fdi, 0, SEEK_SET) != 0) { if(lseek(fdi, 0, SEEK_SET) != 0) {
ret=-1; ret=-1;
} }
/* Read in the header */ /* Read in the header */
if(read(fdi, (void *) header, sizeof(*header)) < 0) { if(read(fdi, (void *) header, sizeof(*header)) < 0) {
ret = -1; ret = -1;
} }
/* Convert header to system endian */ /* Convert header to system endian */
header->unknown = le32toh(header->unknown); header->unknown = le32toh(header->unknown);
header->header_length = le16toh(header->header_length); header->header_length = le16toh(header->header_length);
header->flags = le16toh(header->flags); header->flags = le16toh(header->flags);
header->unknown_zeros = le32toh(header->unknown_zeros); header->unknown_zeros = le32toh(header->unknown_zeros);
header->image_length = le32toh(header->image_length); header->image_length = le32toh(header->image_length);
/* Close the file and check for errors */ /* Close the file and check for errors */
if(close (fdi) < 0) { if(close (fdi) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_save_crc: This function saves the 'CRC' of the Olympus firmware image. /* mr500_save_crc: This function saves the 'CRC' of the Olympus firmware image.
* Note that the 'CRC' must be calculated on the decrytped image. It is stored * Note that the 'CRC' must be calculated on the decrytped image. It is stored
* in little endian. * in little endian.
* *
* Parameters: * Parameters:
* filename: text filename * filename: text filename
* offset: Offset to store the 'CRC' (header size + data size) * offset: Offset to store the 'CRC' (header size + data size)
@ -216,39 +223,39 @@ int mr500_save_crc(char *filename, off_t offset, uint32_t *crc_value) {
int fdo; int fdo;
int ret = 0; int ret = 0;
uint32_t save_crc; uint32_t save_crc;
/* Open the file write only */ /* Open the file write only */
fdo = open(filename, O_WRONLY); fdo = open(filename, O_WRONLY);
if(fdo<0) { if(fdo<0) {
ret=-1; ret=-1;
} }
/* Seek to offset and check for errors */ /* Seek to offset and check for errors */
if(lseek(fdo, offset, SEEK_SET) != offset) { if(lseek(fdo, offset, SEEK_SET) != offset) {
ret=-1; ret=-1;
} }
/* Convert 'CRC' to little endian from system native endian */ /* Convert 'CRC' to little endian from system native endian */
save_crc = htole32(*crc_value); save_crc = htole32(*crc_value);
/* Write the 'CRC' and check for errors */ /* Write the 'CRC' and check for errors */
if(write(fdo, (void *) &save_crc, sizeof(unsigned int)) < 0) { if(write(fdo, (void *) &save_crc, sizeof(unsigned int)) < 0) {
ret = -1; ret = -1;
} }
/* Close the file and check for errors */ /* Close the file and check for errors */
if(close (fdo) < 0) { if(close (fdo) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_read_crc: This function reads the 'CRC' of the Olympus firmware image. /* mr500_read_crc: This function reads the 'CRC' of the Olympus firmware image.
* Note that the 'CRC' is calculated on the decrytped values. It is stored * Note that the 'CRC' is calculated on the decrytped values. It is stored
* in little endian. * in little endian.
* *
* Parameters: * Parameters:
* filename: text filename * filename: text filename
* offset: Offset to read the 'CRC' (header size + data size) * offset: Offset to read the 'CRC' (header size + data size)
@ -260,39 +267,39 @@ int mr500_save_crc(char *filename, off_t offset, uint32_t *crc_value) {
int mr500_read_crc(char *filename, off_t offset, uint32_t *crc_value) { int mr500_read_crc(char *filename, off_t offset, uint32_t *crc_value) {
int fdi; int fdi;
int ret = 0; int ret = 0;
/* Open the file read only */ /* Open the file read only */
fdi = open(filename, O_RDONLY); fdi = open(filename, O_RDONLY);
if(fdi<0) { if(fdi<0) {
ret = -1; ret = -1;
} }
/* Seek to offset and check for errors */ /* Seek to offset and check for errors */
if(lseek(fdi, offset, SEEK_SET) != offset) { if(lseek(fdi, offset, SEEK_SET) != offset) {
ret=-1; ret=-1;
} }
/* Read in the 'CRC' */ /* Read in the 'CRC' */
if(read(fdi, (void *) crc_value, sizeof(uint32_t)) < 0) { if(read(fdi, (void *) crc_value, sizeof(uint32_t)) < 0) {
ret = -1; ret = -1;
} }
/* Convert the 'CRC' from little endian to system native format */ /* Convert the 'CRC' from little endian to system native format */
*crc_value = le32toh(*crc_value); *crc_value = le32toh(*crc_value);
/* Close the file and check for errors */ /* Close the file and check for errors */
if(close (fdi) < 0) { if(close (fdi) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_calculate_crc: This function calculates the 'CRC' of the Olympus /* mr500_calculate_crc: This function calculates the 'CRC' of the Olympus
* firmware image. Note that the 'CRC' must be calculated on decrytped values. * firmware image. Note that the 'CRC' must be calculated on decrytped values.
* It is stored in little endian. * It is stored in little endian.
* *
* Parameters: * Parameters:
* filename: text filename * filename: text filename
* offset: Offset to the start of the data (header size) * offset: Offset to the start of the data (header size)
@ -302,24 +309,24 @@ int mr500_read_crc(char *filename, off_t offset, uint32_t *crc_value) {
* Returns: * Returns:
* Returns 0 if there was no error, -1 if there was an error * Returns 0 if there was no error, -1 if there was an error
*/ */
int mr500_calculate_crc( char *filename, off_t offset, unsigned int length, int mr500_calculate_crc( char *filename, off_t offset, unsigned int length,
uint32_t *crc_value){ uint32_t *crc_value){
uint32_t temp; uint32_t temp;
int fdi; int fdi;
int ret = 0; int ret = 0;
/* Open the file read only */ /* Open the file read only */
fdi = open(filename, O_RDONLY); fdi = open(filename, O_RDONLY);
if(fdi<0) { if(fdi<0) {
ret = -1; ret = -1;
} }
/* Seek to offset and check for errors */ /* Seek to offset and check for errors */
if(lseek(fdi, offset, SEEK_SET) != offset) { if(lseek(fdi, offset, SEEK_SET) != offset) {
ret=-1; ret=-1;
} }
/* Initialize the crc_value to make sure this starts at 0 */ /* Initialize the crc_value to make sure this starts at 0 */
*crc_value = 0; *crc_value = 0;
/* Run this loop till the entire sum is created */ /* Run this loop till the entire sum is created */
@ -329,22 +336,22 @@ int mr500_calculate_crc( char *filename, off_t offset, unsigned int length,
ret = -1; ret = -1;
break; break;
} }
/* Keep summing the values */ /* Keep summing the values */
*crc_value+=temp; *crc_value+=temp;
} while (length-=4); } while (length-=4);
/* Close the file and check for errors */ /* Close the file and check for errors */
if(close (fdi) < 0) { if(close (fdi) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_save_data: This function encypts or decrypts the Olympus firmware /* mr500_save_data: This function encypts or decrypts the Olympus firmware
* image based on the dictionary passed to it. * image based on the dictionary passed to it.
* *
* Parameters: * Parameters:
* source_filename: text filename where data is read from * source_filename: text filename where data is read from
* dest_filename: text filename where data is written to * dest_filename: text filename where data is written to
@ -361,33 +368,33 @@ int mr500_save_data(
int fdi, fdo; int fdi, fdo;
int ret = 0; int ret = 0;
int i; int i;
/* read_count stores the number of bytes actually read */ /* read_count stores the number of bytes actually read */
int read_count; int read_count;
/* read_request stores the number of bytes to be requested */ /* read_request stores the number of bytes to be requested */
int read_request; int read_request;
/* These two buffers are used for reading data and scrambling or /* These two buffers are used for reading data and scrambling or
* descrambling * descrambling
*/ */
int8_t buffer_original[16]; int8_t buffer_original[16];
int8_t buffer_modified[16]; int8_t buffer_modified[16];
/* Open input read only, output write only */ /* Open input read only, output write only */
fdi = open(source_filename, O_RDONLY); fdi = open(source_filename, O_RDONLY);
fdo = open(dest_filename, O_WRONLY); fdo = open(dest_filename, O_WRONLY);
/* If there was an error loading the files set ret appropriately */ /* If there was an error loading the files set ret appropriately */
if(fdi<0 || fdo < 0) { if(fdi<0 || fdo < 0) {
ret = -1; ret = -1;
} }
/* Input file: Seek to offset and check for errors */ /* Input file: Seek to offset and check for errors */
if(lseek(fdi, offset, SEEK_SET) != offset) { if(lseek(fdi, offset, SEEK_SET) != offset) {
ret=-1; ret=-1;
} }
/* Output file: Seek to offset and check for errors */ /* Output file: Seek to offset and check for errors */
if(lseek(fdo, offset, SEEK_SET) != offset) { if(lseek(fdo, offset, SEEK_SET) != offset) {
ret=-1; ret=-1;
@ -403,10 +410,10 @@ int mr500_save_data(
} else { } else {
read_request = length; read_request = length;
} }
/* Read in the data */ /* Read in the data */
read_count = read(fdi, (void *) &buffer_original, read_request); read_count = read(fdi, (void *) &buffer_original, read_request);
/* If there was an error set the flag and break */ /* If there was an error set the flag and break */
if(read_count < 0) { if(read_count < 0) {
ret = -1; ret = -1;
@ -419,14 +426,14 @@ int mr500_save_data(
/* Handle byte scrambling */ /* Handle byte scrambling */
buffer_modified[dictionary[i]] = buffer_original[i]; buffer_modified[dictionary[i]] = buffer_original[i];
} }
/* write the data: If there was an error set the flag and break */ /* write the data: If there was an error set the flag and break */
if(write(fdo, (void *) &buffer_modified, read_count) < 0) { if(write(fdo, (void *) &buffer_modified, read_count) < 0) {
ret = -1; ret = -1;
break; break;
} }
} while (length -= read_count); } while (length -= read_count);
/* Close the files and check for errors */ /* Close the files and check for errors */
if(close (fdi) < 0) { if(close (fdi) < 0) {
ret = -1; ret = -1;
@ -434,12 +441,12 @@ int mr500_save_data(
if(close (fdo) < 0) { if(close (fdo) < 0) {
ret = -1; ret = -1;
} }
return ret; return ret;
} }
/* mr500_init: This function initializes the encryption array /* mr500_init: This function initializes the encryption array
* *
* Parameters: * Parameters:
* None * None
* *