@@ -11,11 +11,21 @@ extern "C" {
1111
1212#include <stdint.h>
1313#include <stdio.h>
14+ #include <cutils/klog.h>
1415#include "boot_img_hdr.h"
1516
1617#define LIBBOOTIMG_VERSION 0x000203 // 0xMMNNPP
1718#define LIBBOOTIMG_VERSION_STR "0.2.3"
1819
20+ #define DEBUG_KMSG
21+ #ifdef DEBUG_KMSG
22+ #define LOG_DBG (fmt , ...) klog_write(3, "<3>%s: " fmt, "libbootimg", ##__VA_ARGS__)
23+ #elif DEBUG_STDOUT
24+ #define LOG_DBG printf("libbootimg: "); printf
25+ #else
26+ #define LOG_DBG (fmt , ...) ""
27+ #endif
28+
1929/**
2030 * Enum containing possible blob types in a boot image.
2131 */
@@ -84,10 +94,12 @@ struct bootimg_blob
8494struct bootimg
8595{
8696 struct boot_img_hdr hdr ; /*!< Boot image header */
97+ struct bootimg_blob blob ; /* Complete blob */
98+ uint32_t blob_size ; /* Size of the complete blob. */
8799 struct bootimg_blob blobs [LIBBOOTIMG_BLOB_CNT ]; /*!< Blobs packed in the boot image. */
88100 int start_offset ; /*!< Offset of the boot image structure from the start of the file. Only used when loading blobs from boot.img file. */
89- struct boot_img_hdr_elf * hdr_elf ; /*!< Boot image header in ELF format */
90101 uint8_t is_elf ; /*!< Select the ELF boot image format */
102+ struct boot_img_elf_info * hdr_info ; /*!< Boot image meta-information for ELF formats. */
91103};
92104
93105/**
@@ -117,6 +129,61 @@ int libbootimg_init_load(struct bootimg *img, const char *path, int load_blob_ma
117129 */
118130int libbootimg_load_header (struct boot_img_hdr * hdr , const char * path );
119131
132+ /**
133+ * Determines the ELF boot image version (custom definition) and the
134+ * required output format (ELF or ANDROID!) based on the number of
135+ * program headers given by the ELF header.
136+ * @param hdr_info pointer to the structure holding the read header information.
137+ */
138+ void libbootimg_get_elf_version (struct boot_img_elf_info * hdr_info );
139+
140+ /**
141+ * Reads the program headers from the given ELF file and adds the content
142+ * of each header to the given structure.
143+ * @param hdr_info structure holding the meta-information of the given ELF file.
144+ * @param f pointer to the file descriptor of the ELF file.
145+ * @return zero on success or the error code returned by the file operations.
146+ */
147+ int libbootimg_load_elf_prog_header (struct boot_img_elf_info * hdr_info , FILE * f );
148+
149+ /**
150+ * Reads the section headers from the given ELF file and adds the content
151+ * of each header to the given structure.
152+ * @param hdr_info structure holding the meta-information of the given ELF file.
153+ * @param f pointer to the file descriptor of the ELF file.
154+ * @return zero on success or the error code returned by the file operations.
155+ */
156+ int libbootimg_load_elf_sect_header (struct boot_img_elf_info * hdr_info , FILE * f );
157+
158+ /**
159+ * Reads the miscellaneous information from the given ELF file and adds the content
160+ * to the given structure. This information is present in some ELF versions and
161+ * required for them to operate properly (version 1).
162+ * @param hdr_info structure holding the meta-information of the given ELF file.
163+ * @param f pointer to the file descriptor of the ELF file.
164+ * @return one on success or the error code returned by the file operations.
165+ */
166+ int libbootimg_load_elf_misc_header (struct boot_img_elf_info * hdr_info , FILE * f );
167+
168+ /**
169+ * Extracts the kernel boot command line from an ELF file and adds it to
170+ * the generic structure describing the blob (-> hdr).
171+ * @param hdr pointer to boot_img_hdr structure
172+ * @param elf_info structure holding the meta-information of the given ELF file.
173+ * @param f pointer to the file descriptor of the ELF file.
174+ */
175+ void libbootimg_read_cmdline (struct boot_img_hdr * hdr , struct boot_img_elf_info * elf_info , FILE * f );
176+
177+ /**
178+ * Returns a pointer referencing the ELF program header which describes the content
179+ * of a given type like the kernel or ramdisk part of a boot image.
180+ * @param hdr_info structure holding the meta-information of the given ELF file.
181+ * @param type integer value that describes the desired pointer as given in the enum
182+ * {@link libbootimg_blob_type}.
183+ * @return pointer to the program header describing the desired part of the boot image.
184+ */
185+ struct boot_img_elf_prog_hdr * get_elf_proc_hdr_of (struct boot_img_elf_info * elf_info , int type );
186+
120187/**
121188 * Loads boot_img_hdr or boot_img_hdr_elf from file on disk
122189 * @param hdr pointer to boot_img_hdr structure
@@ -127,7 +194,8 @@ int libbootimg_load_header(struct boot_img_hdr *hdr, const char *path);
127194 * successful, negative value from libbootimg_error if failed.
128195 */
129196int libbootimg_load_headers (struct boot_img_hdr * hdr ,
130- struct boot_img_hdr_elf * hdr_elf , uint8_t * is_elf , const char * path );
197+ struct boot_img_elf_info * hdr_elf , struct bootimg * img ,
198+ uint8_t * is_elf , const char * path );
131199
132200/**
133201 * Updates the header addresses to the blobs.
@@ -142,8 +210,6 @@ int libbootimg_update_headers(struct bootimg *b);
142210 */
143211void libbootimg_destroy (struct bootimg * b );
144212
145-
146-
147213/**
148214 * Writes blob to a file.
149215 * @param blob pointer to source struct bootimg_blob
@@ -244,6 +310,14 @@ int libbootimg_write_img(struct bootimg *b, const char *dest);
244310 */
245311int libbootimg_write_img_fileptr (struct bootimg * b , FILE * f );
246312
313+ /**
314+ * Writes boot image to a file: Updated implementation that effectively *injects* data into an existing boot image
315+ * @param b pointer to struct bootimg
316+ * @param f pointer to FILE to write data into
317+ * @return number of bytes written to the file if successful, negative value from libbootimg_error if failed.
318+ */
319+ int libbootimg_write_img_fileptr_new (struct bootimg * b , FILE * f );
320+
247321/**
248322 * Writes boot image to a file and then calls libbootimg_destroy.
249323 * The bootimg struct is destroyed even if this function fails.
@@ -274,6 +348,34 @@ const char *libbootimg_version_str(void);
274348 */
275349const char * libbootimg_error_str (int error );
276350
351+ /**
352+ * Prints the content of the boot image information to the stdout or
353+ * the kernel log (dmesg).
354+ * @param hdr pointer to the boot image information.
355+ */
356+ void print_hdr_to_log (struct boot_img_hdr * hdr );
357+
358+ /**
359+ * Prints the content of an elf header described by the given structure
360+ * to the stdout or the kernel log (dmesg).
361+ * @param elf_info pointer to the elf header information.
362+ */
363+ void print_elf_hdr_to_log (struct boot_img_elf_info * elf_info );
364+
365+ /**
366+ * Prints the content of an elf program header described by the given structure
367+ * to the stdout or the kernel log (dmesg).
368+ * @param elf_prog_hdr pointer to the program header information.
369+ */
370+ void print_elf_prog_hdr_to_log (struct boot_img_elf_prog_hdr * elf_prog_hdr );
371+
372+ /**
373+ * Prints the content of an elf section header described by the given structure
374+ * to the stdout or the kernel log (dmesg).
375+ * @param elf_prog_hdr pointer to the section header information.
376+ */
377+ void print_elf_sect_hdr_to_log (struct boot_img_elf_sect_hdr * elf_sect_hdr );
378+
277379#ifdef __cplusplus
278380}
279381#endif
0 commit comments