|
1 | | -// |
2 | | -// Created by sunfl on 25-6-9. |
3 | | -// |
| 1 | +/** @file |
| 2 | +* Create a header or remove a header of a |
| 3 | +* kernel for android boot v1. |
| 4 | +* |
| 5 | +* Copyright (c) 2021-2025 The DuoWoa authors. All rights reserved. |
| 6 | +* MIT License |
| 7 | +* |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "utils.h" |
| 11 | + |
| 12 | +int main( |
| 13 | + int argc, |
| 14 | + char *argv[] |
| 15 | +) { |
| 16 | + // Print hello world message |
| 17 | + printf("Project Aloha Kernel Image HDR Patcher v1.2.0.0\n"); |
| 18 | + printf("Copyright (c) 2021-2025 The DuoWoA authors\n\n"); |
| 19 | + |
| 20 | + // Check parameters and print help message |
| 21 | + if (argc != 3) { |
| 22 | + printf("Usage: <Input Kernel> <Output Kernel>\n"); |
| 23 | + return -EINVAL; |
| 24 | + } |
| 25 | + |
| 26 | + // Check if file exist |
| 27 | + // Init Input file content |
| 28 | + FileContent kernelInput = {.filePath = argv[1]}; |
| 29 | + if (!get_file_size(&kernelInput)) { |
| 30 | + printf("Error: Input kernel file not found or invalid.\n"); |
| 31 | + return -EINVAL; |
| 32 | + } |
| 33 | + kernelInput.fileBuffer = malloc(kernelInput.fileSize + 0x14); |
| 34 | + read_file_content(&kernelInput); |
| 35 | + // add a 0x14 offset of the buffer |
| 36 | + memmove(kernelInput.fileBuffer + 0x14, kernelInput.fileBuffer, kernelInput.fileSize); |
| 37 | + |
| 38 | + // Init Output file content |
| 39 | + FileContent kernelOutput = {.filePath = argv[2]}; |
| 40 | + kernelOutput.fileSize = kernelInput.fileSize; |
| 41 | + kernelOutput.fileBuffer = kernelInput.fileBuffer + 0x14; |
| 42 | + |
| 43 | + // OK now check if kernel has a header |
| 44 | + // if it is, then remove it |
| 45 | + // if it is not, then create a header |
| 46 | + // Header format: |
| 47 | + // 0x00-0x0F: "UNCOMPRESSED_IMG" (16 bytes) |
| 48 | + // 0x10-0x17: Kernel size (8 bytes, little-endian) |
| 49 | + if (strncmp((char *) kernelInput.fileBuffer, "UNCOMPRESSED_IMG", 0x10) == 0) { |
| 50 | + // Kernel has a header, remove it |
| 51 | + printf("Kernel has UNCOMPRESSED_IMG header, removing...\n"); |
| 52 | + kernelOutput.fileBuffer += 0x14; // Move past the header |
| 53 | + kernelOutput.fileSize -= 0x14; // Reduce size by header size |
| 54 | + } else { |
| 55 | + // Kernel does not have a header, create one |
| 56 | + printf("Kernel does not have UNCOMPRESSED_IMG header, creating...\n"); |
| 57 | + // Reallocate buffer to add header |
| 58 | + kernelOutput.fileBuffer -= 0x14; |
| 59 | + // Set header value |
| 60 | + memcpy(kernelOutput.fileBuffer, "UNCOMPRESSED_IMG", 0x10); |
| 61 | + kernelOutput.fileBuffer[0x10] = kernelOutput.fileSize >> 0 & 0xFF; |
| 62 | + kernelOutput.fileBuffer[0x11] = kernelOutput.fileSize >> 8 & 0xFF; |
| 63 | + kernelOutput.fileBuffer[0x12] = kernelOutput.fileSize >> 16 & 0xFF; |
| 64 | + kernelOutput.fileBuffer[0x13] = kernelOutput.fileSize >> 24 & 0xFF; |
| 65 | + kernelOutput.fileSize += 0x14; |
| 66 | + } |
| 67 | + |
| 68 | + // Save the output kernel |
| 69 | + if (write_file_content(&kernelOutput)) { |
| 70 | + printf("Error: Failed to write output kernel file.\n"); |
| 71 | + free(kernelInput.fileBuffer); |
| 72 | + return -EINVAL; |
| 73 | + } |
| 74 | + |
| 75 | + // Free allocated memory |
| 76 | + free(kernelInput.fileBuffer); |
| 77 | + |
| 78 | + // Print success message |
| 79 | + printf("Kernel image processed successfully.\n"); |
| 80 | +} |
0 commit comments