David Oberhollenzer, author of the squashfs-tools-ng toolkit, suggested the concept of self-loading ZIP archives. The project is an adaptation of the self-extracting ZIP archive mechanism to use a ZIP file as a boot image without requiring separate unpacking to boot the system hosted in this image. In this case, the image continues to be a full-fledged ZIP archive, which is verified by the “unzip -t” command.
Unlike the TAR or CPIO formats, the ZIP format contains only a small header at the beginning, and the table of contents of all files and their offsets is stored at the end of the file, which allows you to place arbitrary data almost at the very beginning of the archive. In the presented project, the parameters of the ZIP archive header are selected in such a way that they can be used as an MBR boot sector.
The first bytes of the archive “PK 3 4” 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”. This is followed by a header with version, flags, compression method and modification time, the data in which can be selected so that when executed the contents correspond to harmless instructions and the jmp unconditional jump command, which transfers control to the main bootloader code.
The ZIP file also indicates the signature “1BADB002”, which is defined by bootloaders that support Multiboot as a sign of a compatible boot image (kernel). Thanks to this, the ZIP image can be booted using GRUB or by 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 perceived as a classic MBR.
The entire initial boot code, including the ZIP and Multiboot headers, the GDT table, the logic for loading the archive contents into memory and switching to protected mode, was able to fit into 817 bytes. The files in the ZIP archive are placed without compression (0 is indicated in the compression method field), so after loading the ZIP archive into memory, the files located in it can be accessed directly, using the index located at the end of the ZIP archive (Central Directory) to simulate the simplest file system.