David Oberhollenzer, the author of the squashfs-tools-ng, proposed the concept of self-extracting ZIP archives. The project is an adaptation of the self-extracting ZIP archives mechanism to use a ZIP file as a boot image, which does not require separate extraction to boot the system contained within that image. At the same time, the image remains a full ZIP archive that passes the `unzip -t` command check.
Unlike TAR or CPIO formats, the ZIP format starts with only a small header, while the directory of all files and their offsets is stored at the end of the file. This allows arbitrary data to be placed almost at the beginning of the archive. In the presented project, the parameters of the ZIP archive header are adjusted so that they can be used as a boot sector MBR.
The first bytes of the archive "PK\03\04" are interpreted by the x86 processor as machine code corresponding to the 16-bit instructions of the i8086 processor "push %ax; dec %bx; add (%si),%ax". Following this is a header with version, flags, compression method, and modification time, where the data can be crafted so that, when executed, it corresponds to harmless instructions and an unconditional jump command `jmp`, which transfers control to the main bootloader code.
The ZIP file also specifies the signature "1BADB002", which is recognized by bootloaders supporting Multiboot as an indicator of a compatible boot image (kernel). As a result, the ZIP image can be loaded using GRUB or when specifying the `-kernel` option in QEMU. In this case, the bootloader extracts the uncompressed binary file, places it in memory according to the header, and transfers control in 32-bit protected mode. The ZIP image can also be written to the first sector of the hard drive, in which case the first 512 bytes are interpreted as a classic MBR.
The entire initial boot code, including the ZIP and Multiboot headers, GDT table, the logic for loading the archive content into memory, and transitioning to protected mode, fit into 817 bytes. The files in the ZIP archive are placed without compression (the compression method field indicates 0), so after loading the ZIP archive into memory, the files contained within can be accessed directly by using the index (Central Directory) found at the end of the ZIP archive to simulate a basic file system.
Source: opennet.ru
