/** * @file cpio.h * @brief CPIO newc archive parser. * * Parses CPIO archives in the SVR4/newc format (magic "070701"). * Used to read files from the initial ramdisk loaded by GRUB. */ #ifndef CPIO_H #define CPIO_H #include #include /** * CPIO newc header (110 bytes). * All fields are 8-character hexadecimal ASCII strings. */ typedef struct cpio_newc_header { char magic[6]; /**< Must be "070701". */ char ino[8]; char mode[8]; char uid[8]; char gid[8]; char nlink[8]; char mtime[8]; char filesize[8]; char devmajor[8]; char devminor[8]; char rdevmajor[8]; char rdevminor[8]; char namesize[8]; char check[8]; } cpio_newc_header_t; /** Size of the CPIO newc header in bytes. */ #define CPIO_HEADER_SIZE 110 /** CPIO newc magic string. */ #define CPIO_MAGIC "070701" /** Trailer entry name that marks end of archive. */ #define CPIO_TRAILER "TRAILER!!!" /** * CPIO file entry (result of iteration or lookup). */ typedef struct cpio_entry { const char *name; /**< Filename (pointer into archive). */ uint32_t namesize; /**< Length of filename including NUL. */ const void *data; /**< Pointer to file data within archive. */ uint32_t datasize; /**< Size of file data in bytes. */ uint32_t mode; /**< File mode/permissions. */ } cpio_entry_t; /** * Initialize the CPIO parser with the archive location. * * @param archive_start Pointer to the start of the CPIO archive in memory. * @param archive_size Size of the archive in bytes (0 if unknown). */ void cpio_init(const void *archive_start, uint32_t archive_size); /** * Find a file in the CPIO archive by name. * * @param name Filename to search for (without leading "./"). * @param entry Output: filled with file information if found. * @return 0 on success, -1 if not found. */ int cpio_find(const char *name, cpio_entry_t *entry); /** * Iterate through all entries in the CPIO archive. * * Call with *offset = 0 to start. Returns 0 on success, -1 when no * more entries exist or the TRAILER is reached. * * @param offset In/out: current position in the archive. * @param entry Output: filled with the next entry's information. * @return 0 on success, -1 at end of archive. */ int cpio_next(uint32_t *offset, cpio_entry_t *entry); /** * Get the number of files in the CPIO archive (excluding TRAILER). * * @return Number of files. */ uint32_t cpio_count(void); #endif /* CPIO_H */