{"article":{"slug":"creating-a-self-booting-zip-archive","title":"Creating a self booting ZIP archive","subtitle":null,"summary":"David Oberhollenzer builds boot.zip, a completely normal ZIP archive that contains its own source code and is also bootable via BIOS and Multiboot, walking through ZIP format polyglot trickery, the Multiboot header and booting on bare metal.","content_type":"tutorial","language":"en","canonical_url":"https://goliath32.com/blog/ziphead.html","author":{"name":"David Oberhollenzer","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"goliath32.com","url":"https://goliath32.com/","listing_slug":null,"listing":null},"topics":[{"name":"systems-programming","slug":"systems-programming","url":"https://listedarticles.com/topics/systems-programming"},{"name":"linux","slug":"linux","url":"https://listedarticles.com/topics/linux"},{"name":"reverse-engineering","slug":"reverse-engineering","url":"https://listedarticles.com/topics/reverse-engineering"},{"name":"programming","slug":"programming","url":"https://listedarticles.com/topics/programming"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":3879,"reading_minutes":17,"published_at":"2026-11-14T11:13:20.295Z","added_at":"2026-11-14T11:13:20.295Z","updated_at":"2026-11-14T11:13:20.295Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/creating-a-self-booting-zip-archive","markdown_url":"https://listedarticles.com/articles/creating-a-self-booting-zip-archive.md","example":false,"citation":"David Oberhollenzer, goliath32.com. \"Creating a self booting ZIP archive.\" 14 Nov 2026. https://goliath32.com/blog/ziphead.html (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://goliath32.com/blog/ziphead.html"},"body_markdown":"# Creating a self booting ZIP archive\n\n---\n\n## Introduction\n\nSo, after a debugging session the other day, I was having some shower\nthoughts regarding early userspace and initrd.\n\nLinux initramfs supports unpacking CPIO, a stupid, simple, standardized\nformat. In the discussion on whether to support TAR instead, much\nemphasize has been put on the fact that CPIO is standardized and less\nfragmented.\n\nLuckily, Linux (the kernel) includes its own tool to produce the specific\nvariant of the standard that it happens to implement. The standard CPIO\ntool is possibly the most cursed archiver under the SUN.\n\nIn the old DOS/WinDOS world, ZIP was probably the most wide spread and\nwell known archive format, being displaced to some extend by the rise of\nRAR during the 2000s.\n\nBecause of the design of the format, ZIP lends itself to all kinds of\npolyglot trickery. Way back when, self extracting ZIP archives where a\nthing, EXE/ZIP polyglots that can be run and unpack themselves, which is\nuseful if you don't have an archiver installed.\n\nI don't think I need to explain where I am going with this, here is the\nresulting ZIP archive I built:\n\n[![](https://goliath32.com/img/download.gif)Download boot.zip](https://goliath32.com/sw/boot.zip)\n\nIt is a completely normal ZIP archive, containing its own source code\nneeded to build itself from scratch:\n\n```\n$ unzip -t boot.zip\nArchive:  boot.zip\n    testing: boot/ZIPHEAD.COM         OK\n    testing: boot/MESSAGE.TXT         OK\n    testing: common/asm.h             OK\n    testing: common/multiboot.h       OK\n    testing: common/zip.h             OK\n    testing: multiboot/console.c      OK\n    testing: multiboot/console.h      OK\n    testing: multiboot/multiboot.c    OK\n    testing: multiboot/string.S       OK\n    testing: setup/a20.S              OK\n    testing: setup/disk.S             OK\n    testing: setup/entry16.S          OK\n    testing: setup/entry32.S          OK\n    testing: setup/mbload.S           OK\n    testing: setup/mem.S              OK\n    testing: setup/setup.ld           OK\n    testing: util/fixhdr.c            OK\n    testing: util/zipglue.c           OK\n    testing: util/zippack.c           OK\n    testing: mk.sh                    OK\n    testing: README.txt               OK\n    testing: MESSAGE.txt              OK\n    testing: LICENSE.txt              OK\nNo errors detected in compressed data of boot.zip.\n```\n\nA particularly interesting file here is **boot/ZIPHEAD.COM**, that\ncontains executable code that is run when booting the archive.\n\nYou need to fix the execute permissions of mk.sh after\nunpacking, and you will need a copy of Zlib installed for\nthe tools. Running mk.sh from it's directory should\nleave you with a fresh copy of the bootable ZIP archive.\n\nThe simplest way to run it, is to just pass it to Qemu direct kernel boot:\n\n```\n$ qemu-system-i386 -kernel boot.zip\n```\n\n\"That's easy\" you might say, \"Qemu supports Multiboot, that ZIPHEAD.COM\nis uncompressed, at the beginning of the archive and has a Multiboot\nheader in it!\"\n\nYes, that's all true! But this ZIP archive also brings it's own boot\nloader along. You can try putting it on a floppy and throw that in front\nof Qemu:\n\n```\n$ dd if=/dev/zero of=floppy.img count=2880\n$ dd if=boot.zip of=floppy.img conv=notrunc\n$ qemu-system-i386 -fda floppy.img\n```\n\nThe first 512 bytes of the archive contain a valid boot sector that\nloads some more code from the second sector and then bootstraps itself\ninto highmem with the A20 enabled and 32 bit protected mode set up.\n\nYou can also put it on a hard drive too, it detects what **INT 13h**\ndrive it's been loaded from and dutifully queries the disk geometry.\n\nIf you care to try, you can also dump it on a real floppy and stuff it\ninto a PC:\n\n[![](https://goliath32.com/img/prev/blog/ziphead/0.jpg)](https://goliath32.com/img/photo/blog/ziphead/0.jpg)\n\nThat's a Pentium MMX by the way, but I also tried an i486 DX2 and an\nAm386DX-40, just to be sure. My 486 had me hold my breath for a moment,\nit has a spectacularly slow floppy drive. The text on screen is from\nthe file **boot/MESSAGE.TXT**\n\n\"But that's not special either\" you might respond, \"ZIPs are read from\nthe back, you can just put anything you want in front, so you put a boot\nsector there!\"\n\nThat is sadly *not quite true* anymore, I'll get to that. But for\nnow, lets take a look at the first couple bytes of that archive\ntogether and we'll see the real party trick:\n\n```\n$ xxd -g 1 -l 128 boot.zip\n00000000: 50 4b 03 04 0a 00 02 80 00 00 eb 44 e4 2e 41 0f  PK.........D..A.\n00000010: 01 8f 0a 09 00 00 0a 09 00 00 10 00 00 00 62 6f  ..............bo\n00000020: 6f 74 2f 5a 49 50 48 45 41 44 2e 43 4f 4d 00 90  ot/ZIPHEAD.COM..\n00000030: 02 b0 ad 1b 03 00 01 00 fb 4f 51 e4 30 00 10 00  .........OQ.0...\n00000040: 00 00 10 00 bd 72 10 00 bd 72 10 00 31 03 10 00  .....r...r..1...\n00000050: fa fc 31 c0 89 c4 8e d8 8e c0 8e d0 ea 61 7c 00  ..1..........a|.\n00000060: 00 bf 00 06 b9 00 02 f3 aa 88 16 98 06 be 0f 7d  ...............}\n00000070: e8 8c 00 b8 00 f0 89 c1 50 9d 9c 58 a8 02 74 4a  ........P..X..tJ\n```\n\nThe ASCII **P** stands for Phil\n([not Picard](https://memory-alpha.fandom.com/wiki/Lonely_Among_Us_(episode)#Act_Five)), as in\n[Phil Katz](https://en.wikipedia.org/wiki/Phil_Katz),\nfollowed by **03h** and **04h**. What you are looking at, is a\ncompletely valid ZIP local file header for **boot/ZIPHEAD.COM**\n\nOh, and those other bytes there after the ZIP header: **02 b0 ad 1b**,\nif you flip them around to human endian, it reads **1BADB002**.\nThat's the Multiboot magic :-)\n\nThe 16 bit real mode code starts right after the Multiboot header,\nat offset **00000050**. If you are feeling a little confused,\nso is the file utility:\n\n```\n$ file boot.zip\nboot.zip: DOS/MBR boot sector; partition 1 : ID=0xd, active 0xcd, start-CHS (0x7\n2,19,7), end-CHS (0x388,254,6), startsector 2147916342, 243810273 sectors; parti\ntion 2 : ID=0x60, active 0x9c, start-CHS (0x366,6,3), end-CHS (0x3f7,49,18), sta\nrtsector 2298911798, 4154016209 sectors; partition 4 : ID=0xf8, active 0xe1, sta\nrt-CHS (0x2,184,1), end-CHS (0x313,6,13), startsector 3277940231, 0 sectors\n```\n\nIf you take a closer look at the first line of the hex dump, you might\nbe able to spot the jump instruction that makes this little magic trick\npossible. Luckily, Phil Katz' initials are valid opcodes. The Multiboot\nentry point is much further into the file at offset **00000331**.\n\n## ZIP Archives Revisited\n\n![](https://goliath32.com/img/blog/ziphdr.svg)\n\nHere's a picture you probably recognize from\n[the Wikipedia article about the ZIP format](https://en.wikipedia.org/wiki/ZIP_(file_format)).\nThis here is a local copy, Wikimedia Commons hosts\n[the original with attribution and licensing information](https://commons.wikimedia.org/wiki/File:ZIP-64_Internal_Layout.svg).\n\nMuch like a TAR or CPIO archive, a ZIP file is an amalgamation of tightly\npacked files with a short header in front, specifying the full file path\nand other meta data:\n\n```\ntypedef struct {\n\tuint32_t magic;\n\tuint16_t version;\n\tuint16_t flags;\n\tuint16_t method;\n\tuint16_t modtime;\n\tuint16_t moddate;\n\tuint32_t checksum;\n\tuint32_t compsize;\n\tuint32_t origsize;\n\tuint16_t namelength;\n\tuint16_t extralength;\n\tchar name[namelength];\n\tuint8_t extra[extralength];\n} __attribute__((packed)) zip_entry_t;\n```\n\nThe timestamps are DOS datetime, the checksum is a CRC-32, there are no\nattributes or permission bits, we are thoroughly in DOS land. Directories\nare implied by the file paths.\n\nNit pick\nYes, I know, the central directory has a field where \"internal\" and\n\"external\" attributes can be stored, whose meaning depends on the OS ID\nbyte in the version field. Later versions of the format added an\nextension to support directories. Good luck making use of that in a\nportable way that can be understood by different archivers across\ndifferent WinDOS versions and Unix clones.\n\nUnlike TAR or CPIO, there is a **central directory** in the end, with\na listing of all of the file headers and back references to the file in\nthe archive. Unlike traditional file formats that have are\n**started by a header**, ZIP is **terminated by a footer**.\n\nThe reason for this is twofold:\n\n- the archive can be generated in a single, forward pass (collecting\n  the directory in memory).\n- Accessing a single file in the archive can be done quickly, by\n  looking through the directory and going directly to the file.\n\nThat last part was especially important in the days when ZIP archives\nwould be split over several floppies. To access a file, just scan the\ndirectory on the last floppy, then tell the user what floppy to insert to\nget the file.\n\nBy comparison, accessing a file in a tarball requires linearly scanning\nover the whole thing (which, on the other hand, is\n[preferable for tape drives](https://en.wikipedia.org/wiki/Tape_drive#Technical_limitations), where TAR originates).\n\n### Polyglot Trickery\n\nZIP archives are meant to be read from the back, the footer points to the\ncentral directory, the directory points to the files. *In theory*,\nthe archiver shouldn't care if there happens to be a gap between two\nfiles, containing something else or if there is something else in the\nfront of the archive, like a file header for a different file format.\n\nThe local file headers also supports up to 64k of \"extra\" stuff between\nthe header and the file data, the footer has \"comment\" at the end, also\nup to 64k.\n\nIf you look at the footer for a second, you might spot an interesting\ndesign flaw:\n\n```\ntypedef struct {\n\tuint32_t magic;\n\tuint16_t disknum;\n\tuint16_t diskstart;\n\tuint16_t num_dir_entries;\n\tuint16_t total_dir_entries;\n\tuint32_t dir_size;\n\tuint32_t dir_start;\n\tuint16_t comment_length;\n\tuint8_t comment[comment_length];\n} __attribute__((packed)) zip_end_t;\n```\n\nit is variable size, an archiver needs to find it looking\nfrom the back, but the magic and the size are both stored *before*\nthe variable sized comment field. Ask yourself this for a moment: how\nwould you implement this in an archiver? Demanding that there is no\ncomment? Reading the last 64k plus some change and scanning it? From\nwhich direction? What if there is another valid footer inside the\ncomment field of the footer? Depending on how an implementation deals\nwith this, a specifically crafted archive can appear to contain\ndifferent files when viewed with different archivers.\n\nTrying to make the format extensible ended up building a whole lot of\nfoot guns in there. And we are not even talking about the plethora of\nextensions that have been tacked on to the official specification over\nthe years.\n\nMany modern implementations decided to save themselves a lot of headaches\nand just treat ZIP archives the same way as tarballs, linearly scanning\nthem from the front instead. They expect the archive to start with a\nlocal header, skipping by the size **MUST** lead to the next entry\nor finally, the central directory. They reject any archives where this\nis not possible. This effectively prevents a lot of polyglot trick\nfuckery, but at the cost of rendering possibly valid archives unreadable.\n\nFor the self booting ZIP archive, the challenge thus becomes: the archive\n**MUST** start with a local file header, there cannot be *any*\ngaps, everything must be properly contained inside files.\n\n## The easy part: Multiboot\n\nAnybody who has ever looked at a DIY toy OS tutorial has probably\nstumbled across\n[The Multiboot Specification](https://www.gnu.org/software/grub/manual/multiboot/multiboot.html), the possibly easiest and most beginner friendly way to get\na program running on bare metal x86.\n\nThe multiboot spec originates in the GNU/FSF ecosystem and is implemented\nby boot loaders like GRUB or Qemu direct kernel boot. SYSLINUX supports\nit via a plugin. There is a draft for an over engineered 2nd version, that\nalso supports MIPS, but this is of no concert to us here.\n\nA Multiboot 1 compliant kernel simply contains the magic\nvalue **0x1BADB002** *somewhere* in the first in the first 8k.\nThis is followed by flags and a checksum (all 3 must add up to zero).\nDepending on the flags, additional fields may follow.\n\nThe kernel itself can be an ELF file from which the loader extracts\nload address and entry point. The kernel is run in 32 bit protected\nmode with flat segments (base=0, limit=4G) and the A20 gate enabled.\n\nNit pick\nThe specification actually doesn't mandate ELF, but rather \"an ordinary\n32-bit executable file in whatever file format the operating system\nnormally uses.\", it then goes on to specify **optional** fields in\nthe info struct for either **ELF** or **a.out**. In theory, it\nshould also be possible for a boot loader to only support **PE** and\nnot be in violation of the spec.\n\nAlternatively to a plain old ELF file, the kernel can also specify the\nmemory layout manually (indicated via a flag), allowing flat binaries or\nother file formats to be loaded. The extended header looks like this:\n\n```\ntypedef struct {\n\tuint32_t magic;\n\tuint32_t flags;\n\tuint32_t checksum;\n\tuint32_t hdr_addr;\n\tuint32_t load_addr;\n\tuint32_t load_end_addr;\n\tuint32_t bss_end_addr;\n\tuint32_t entry_point;\n} mb_header_t;\n```\n\nThe **hdr\\_addr** specifies the in-memory address of the header after\nloading the file, using this and the file offset where the header was\nfound, the other addresses can be converted back to file offsets by the\nloader.\n\nThis way, the **load\\_addr** and **load\\_end\\_addr** are used to figure\nout *what* parts of the file to load *where* into memory. If the\n**bss\\_end\\_addr** is past that, the loader zero-fills it, before finally\njumping to **entry\\_point** to run the kernel.\n\nThe Multiboot compliant loader also sets up a structure with system\ninformation (e.g. memory map, APM tables), kernel command line,\netc… and passes the pointer to the kernel (via register\n**EBX**, magic value **0x2BADB002** in **EAX**).\n\nMaking a Multiboot bootable ZIP archive is trivial. Simply pack a flat\nbinary with a Multiboot header as uncompressed file binary, make sure it\nis at the beginning and the addresses line up. An external tool can then\nfix up the headers, so that the whole archive is loaded.\n\nFor any *practical* applications, it would make *sense* to stop\nright here, but we're not here for \"practical\" or \"sane\".\n\n## Making it BIOS bootable\n\nOn an IBM compatible machine, the BIOS iterates over the installed\ndrives, loads the first sector from each (to location **0x7c00**),\nuntil it finds one where the first sector ends with **0xaa55**.\nIt then jumps to the first byte.\n\nDisks that are formatted with a filesystem, typically have a header in\nthe beginning that starts with a jump instruction across the header into\nthe bootstrap code that follows. The DOS MBR format opts to instead store\nthe partition table at the end of the first sector and all the code in\nthe front.\n\nIf we stuff a BIOS boot signature into the ZIP archive, at least parts\nof the ZIP local file header will be executed, the first few bytes are\na mandated magic value and we cannot just put a jump instruction there.\n\nThe puzzle exercise now becomes, filling the header fields with values\nthat make sense but also happen to be valid 8086 opcodes. At least,\nuntil we hit something that can be abused as a free-form field that we\ncan stuff a jump into.\n\nLet's take a look at that hex dump again:\n\n```\n00000000: 50 4b 03 04 0a 00 02 80 00 00 eb 44 e4 2e 41 0f  PK.........D..A.\n00000010: 01 8f 0a 09 00 00 0a 09 00 00 10 00 00 00 62 6f  ..............bo\n00000020: 6f 74 2f 5a 49 50 48 45 41 44 2e 43 4f 4d 00 90  ot/ZIPHEAD.COM..\n```\n\nI've tried to highlight the relevant header up to the jump\ninstruction: **0xeb** is a relative jump with a single byte\nimmediate argument.\n\nIf we take a look again at the header structure, we can make out what\nfields those bytes correspond to:\n\n```\ntypedef struct {\n\tuint32_t magic;\n\tuint16_t version;\n\tuint16_t flags;\n\tuint16_t method;\n\tuint16_t modtime;\n\t………\n```\n\nThe timestamp is the first best place where we can put a jump\ninstruction. The **0xeb** relative jump works out to **8:30-ish**\nin the morning, give or take the jump distance.\n\nThe two bytes in front of the **modtime** are compression\n**method**. We really, really want to have **0x0000** here,\ni.e. *uncompressed*, so that an archiver doesn't choke on the\nfile data.\n\nThe first four bytes are the magic value of the format, we don't have any\nchoice what to put here either, it has to be \"**PK\\03\\04**\". The\n**version** and **flags** fields are the only places where we have\nsome creative freedom.\n\nIf we run the file through a dissassembler and take a look at the\nfirst few bytes, we'll end up with this:\n\n```\n$ objdump -D -m i8086 -b binary ./boot.zip\n\n./boot.zip:     file format binary\n\nDisassembly of section .data:\n\n00000000 <.data>:\n       0:       50                      push   %ax\n       1:       4b                      dec    %bx\n       2:       03 04                   add    (%si),%ax\n       4:       0a 00                   or     (%bx,%si),%al\n       6:       02 80 00 00             add    0x0(%bx,%si),%al\n       a:       eb 44                   jmp    0x50\n```\n\nThis is AT&T syntax, source operands are left, destination\non the right. We use GNU tools in this household, deal with it.\n\nAnything that just fiddles around with a register doesn't really hurt us.\nSame goes for memory *reads*. We don't have memory protection, no\nmatter what we access, there won't be any faults, the only memory\nmapped hardware around should be the frame buffer.\n\nMemory writes are another story. Worst case, we could shoot up the\ninterrupt vectors, the BIOS RAM areas (BDA, EBDA), or shadowed option\nROMs.\n\nThe **P** for Phil also happens to also decode to\n**push %ax**. If the BIOS leaves us with a somewhat sane stack\npointer, the **push** instruction should hopefully not mess anything\nup. It is part of the ZIP magic and thus unavoidable.\n\nThe **version** field is itself actually *structured*, containing\na decimal encoded version number, followed by an OS ID byte. If we set\nit to **0x000a** (stored as little endian), this decodes to version\n**1.0** on **MS DOS**.\n\n**0x0a** is an **orb**, version 1.1 would be double byte sized,\nversion 0.8 and 0.9 would be register direct and not touch the memory.\nAn address of **BX + SI** is the result of a **mod-reg-r/m** field\nwith all zeros.\n\nWe could try to code golf our way into consuming at least part of the\n**flags** as well, but low-valued, \"harmless\" opcodes that result\nin sane versions are sparse. Also, some version numbers change the\nmeaning how an archiver is supposed to interpret the file. Also also,\n**mod-reg-r/m** combinations in the OS code field quickly end up as\nan undefined OS ID. Also also also, we have the **method** as well,\nthat *has to be* **0x0000**, which would result in a memory\nwrite if executed. We have to consume that somehow, rather than running\nit.\n\nSo for the **flags**, I came up with **0x8002** (stored as little\nendian). The lowest, set bit has a compression method dependent meaning,\ncurrently only defined for LZMA (14), deflate (8) and implode (6). For\nuncompressed (method 0) there is no defined meaning (the specification\njust ignores that part, it doesn't use nasty words like\n*undefined*). For deflate it would mean that\n*maximum compression* was used.\n\nThe MSB is specified as \"reserved for use by PKWARE\", whatever they chose\nto put there, if they ever chose anything at all. Most imporanly, the\nopcode **0x02** decodes to a single byte, indirect add, and in the\n**mod-reg-r/m**, the MSB tells the CPU to consume another 2 bytes of\nimmediate **DISP**lacement. This is how we can get rid of the\ncompression method.\n\nAt this point, we've made it! We reached the **modtime** field\nthat we can just stuff a jump into.\n\n## Drawing the rest of the owl: Multiboot on foot\n\nOnce we've made it past the header, we are in 16 bit real mode. We just\nneed to go through all of the neccessary steps to set up the environment\nthat the Multiboot code expects:\n\n- Initialization (setup segments, clear scratch memory)\n- Figure out what kind of CPU we are running on\n\n- We need *at least* an i386, so we\n  can *actually run* 32 bit code\n- The **CPUID** instruction was added to the Pentium and later 486,\n  so we can't use that\n- We need to tickle undefined bits in the **FLAGS** register\n  and watch what happens\n\n- Query disk parameters, load additional boot code past the\n  first sector\n  - We need the C/H/S geometry to sanely read past the first track\n  - For simplicity, work with LBA and convert to C/H/S bit confetti for\n    **INT 13h** as needed\n- Detect available memory\n  - Memory below 1M via **INT 12h**\n  - If supported, high memory via **INT 15h**, **AX=E801h** to\n    query memory before and after the (possible) ISA memory hole\n    at 15M\n  - If that fails, fallback to **INT 15h**, **AH=88h** to\n    get a lump sum of available high memory\n  - If supported, get a detailed memory map\n    via **INT 15h**, **AX=E820h**. This already uses the format\n    specified by the Multiboot spec, just slap an extra size value in\n    front of every entry.\n- Turn on the A20 gate\n  - Check if memory location **0000:0600** (=0x0600) and\n    **ffff:0610** (=0x100600) overlap or not\n  - Try BIOS **INT 15h**, **AX=2401h**\n  - If that fails, try talking to the keyboard controller directly\n  - If that fails, try SYSCTL A register\n- Load a GDT and set up 16 bit unreal mode\n  - To *actually access* high memory, we need to\n    get rid of the 64k segment limits\n  - We need to stay in *real-ish* mode, so we can\n    keep using **INT 13h**\n- Load the rest of the archive into high memory\n- Jump to the Multiboot entry point\n  - Re-enter protected mode, 32 bit code segment\n  - Cobble the rest of the Multiboot info struct together\n  - Leap-of-faith into highmem\n\nThis might sound like a lot, but in the current version, the actual code\nboils down to 817 bytes. That includes all the headers (ZIP, multiboot),\nthe rather bulky GDT. But it is still quite a lot and outside the scope\nof this post anyway, I might pick some interesting parts out of it for\nsome follow-up posts (watch this space?).\n\n## Through the Looking Glass\n\nThe Multiboot entry code re-loads the GDT, but this time using the\nhighmem location, and sets up a stack after the archive. The size is\nextracted from the Multiboot header. Knowing the start address, the C\ncode can then iterate over itself to locate and access files in the\narchive.\n\nSome linker script trickery is used to tie it all into one blob, but with\nthe GDT and Multiboot header visible from both lomem and highmem code at\ndifferent locations.\n\nAt this point, you might be inclined to actually read the source code\nin the ZIP archive. There are lots of comments trying to explain what\nis going on.\n\nThere's a README that points out the structure of the code with\nsuggestions where to start reading. I decided to keep it readable rather\nthan stuff it all into a single sector. Self-modifying trickery is kept\nto a minimum (to safe some space, the function to enter unreal mode\nmodifies itself, the code is run twice, courtesy of a misdirected\n**ret**, entering protected mode the first time round, leaving it\nagain and restoring the segment registers on the second run, before\nreturning for real).\n\nCode in the archive and this blog posts have been written with\nassistance from:\n\n- Tegernseer Hell\n- [Gouryella](https://www.youtube.com/watch?v=NEivFBrGESk)\n  et al\n\nIf you poke around in the code and have any suggestions for improvements,\nor otherwise do anything interesting with it, please let me know.\n\nHave fun!\n","body_html":"<h1 id=\"creating-a-self-booting-zip-archive\">Creating a self booting ZIP archive</h1>\n<hr />\n<h2 id=\"introduction\">Introduction</h2>\n<p>So, after a debugging session the other day, I was having some shower\nthoughts regarding early userspace and initrd.</p>\n<p>Linux initramfs supports unpacking CPIO, a stupid, simple, standardized\nformat. In the discussion on whether to support TAR instead, much\nemphasize has been put on the fact that CPIO is standardized and less\nfragmented.</p>\n<p>Luckily, Linux (the kernel) includes its own tool to produce the specific\nvariant of the standard that it happens to implement. The standard CPIO\ntool is possibly the most cursed archiver under the SUN.</p>\n<p>In the old DOS/WinDOS world, ZIP was probably the most wide spread and\nwell known archive format, being displaced to some extend by the rise of\nRAR during the 2000s.</p>\n<p>Because of the design of the format, ZIP lends itself to all kinds of\npolyglot trickery. Way back when, self extracting ZIP archives where a\nthing, EXE/ZIP polyglots that can be run and unpack themselves, which is\nuseful if you don&#39;t have an archiver installed.</p>\n<p>I don&#39;t think I need to explain where I am going with this, here is the\nresulting ZIP archive I built:</p>\n<p><a href=\"https://goliath32.com/sw/boot.zip\" rel=\"nofollow ugc noopener\"><img src=\"https://goliath32.com/img/download.gif\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" />Download boot.zip</a></p>\n<p>It is a completely normal ZIP archive, containing its own source code\nneeded to build itself from scratch:</p>\n<pre><code>$ unzip -t boot.zip\nArchive:  boot.zip\n    testing: boot/ZIPHEAD.COM         OK\n    testing: boot/MESSAGE.TXT         OK\n    testing: common/asm.h             OK\n    testing: common/multiboot.h       OK\n    testing: common/zip.h             OK\n    testing: multiboot/console.c      OK\n    testing: multiboot/console.h      OK\n    testing: multiboot/multiboot.c    OK\n    testing: multiboot/string.S       OK\n    testing: setup/a20.S              OK\n    testing: setup/disk.S             OK\n    testing: setup/entry16.S          OK\n    testing: setup/entry32.S          OK\n    testing: setup/mbload.S           OK\n    testing: setup/mem.S              OK\n    testing: setup/setup.ld           OK\n    testing: util/fixhdr.c            OK\n    testing: util/zipglue.c           OK\n    testing: util/zippack.c           OK\n    testing: mk.sh                    OK\n    testing: README.txt               OK\n    testing: MESSAGE.txt              OK\n    testing: LICENSE.txt              OK\nNo errors detected in compressed data of boot.zip.</code></pre>\n<p>A particularly interesting file here is <strong>boot/ZIPHEAD.COM</strong>, that\ncontains executable code that is run when booting the archive.</p>\n<p>You need to fix the execute permissions of mk.sh after\nunpacking, and you will need a copy of Zlib installed for\nthe tools. Running mk.sh from it&#39;s directory should\nleave you with a fresh copy of the bootable ZIP archive.</p>\n<p>The simplest way to run it, is to just pass it to Qemu direct kernel boot:</p>\n<pre><code>$ qemu-system-i386 -kernel boot.zip</code></pre>\n<p>&quot;That&#39;s easy&quot; you might say, &quot;Qemu supports Multiboot, that ZIPHEAD.COM\nis uncompressed, at the beginning of the archive and has a Multiboot\nheader in it!&quot;</p>\n<p>Yes, that&#39;s all true! But this ZIP archive also brings it&#39;s own boot\nloader along. You can try putting it on a floppy and throw that in front\nof Qemu:</p>\n<pre><code>$ dd if=/dev/zero of=floppy.img count=2880\n$ dd if=boot.zip of=floppy.img conv=notrunc\n$ qemu-system-i386 -fda floppy.img</code></pre>\n<p>The first 512 bytes of the archive contain a valid boot sector that\nloads some more code from the second sector and then bootstraps itself\ninto highmem with the A20 enabled and 32 bit protected mode set up.</p>\n<p>You can also put it on a hard drive too, it detects what <strong>INT 13h</strong>\ndrive it&#39;s been loaded from and dutifully queries the disk geometry.</p>\n<p>If you care to try, you can also dump it on a real floppy and stuff it\ninto a PC:</p>\n<p><a href=\"https://goliath32.com/img/photo/blog/ziphead/0.jpg\" rel=\"nofollow ugc noopener\"><img src=\"https://goliath32.com/img/prev/blog/ziphead/0.jpg\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>That&#39;s a Pentium MMX by the way, but I also tried an i486 DX2 and an\nAm386DX-40, just to be sure. My 486 had me hold my breath for a moment,\nit has a spectacularly slow floppy drive. The text on screen is from\nthe file <strong>boot/MESSAGE.TXT</strong></p>\n<p>&quot;But that&#39;s not special either&quot; you might respond, &quot;ZIPs are read from\nthe back, you can just put anything you want in front, so you put a boot\nsector there!&quot;</p>\n<p>That is sadly <em>not quite true</em> anymore, I&#39;ll get to that. But for\nnow, lets take a look at the first couple bytes of that archive\ntogether and we&#39;ll see the real party trick:</p>\n<pre><code>$ xxd -g 1 -l 128 boot.zip\n00000000: 50 4b 03 04 0a 00 02 80 00 00 eb 44 e4 2e 41 0f  PK.........D..A.\n00000010: 01 8f 0a 09 00 00 0a 09 00 00 10 00 00 00 62 6f  ..............bo\n00000020: 6f 74 2f 5a 49 50 48 45 41 44 2e 43 4f 4d 00 90  ot/ZIPHEAD.COM..\n00000030: 02 b0 ad 1b 03 00 01 00 fb 4f 51 e4 30 00 10 00  .........OQ.0...\n00000040: 00 00 10 00 bd 72 10 00 bd 72 10 00 31 03 10 00  .....r...r..1...\n00000050: fa fc 31 c0 89 c4 8e d8 8e c0 8e d0 ea 61 7c 00  ..1..........a|.\n00000060: 00 bf 00 06 b9 00 02 f3 aa 88 16 98 06 be 0f 7d  ...............}\n00000070: e8 8c 00 b8 00 f0 89 c1 50 9d 9c 58 a8 02 74 4a  ........P..X..tJ</code></pre>\n<p>The ASCII <strong>P** stands for Phil\n(<a href=\"https://memory-alpha.fandom.com/wiki/Lonely_Among_Us_(episode)#Act_Five\" rel=\"nofollow ugc noopener\">not Picard</a>), as in\n<a href=\"https://en.wikipedia.org/wiki/Phil_Katz\" rel=\"nofollow ugc noopener\">Phil Katz</a>,\nfollowed by **03h</strong> and <strong>04h</strong>. What you are looking at, is a\ncompletely valid ZIP local file header for <strong>boot/ZIPHEAD.COM</strong></p>\n<p>Oh, and those other bytes there after the ZIP header: <strong>02 b0 ad 1b</strong>,\nif you flip them around to human endian, it reads <strong>1BADB002</strong>.\nThat&#39;s the Multiboot magic :-)</p>\n<p>The 16 bit real mode code starts right after the Multiboot header,\nat offset <strong>00000050</strong>. If you are feeling a little confused,\nso is the file utility:</p>\n<pre><code>$ file boot.zip\nboot.zip: DOS/MBR boot sector; partition 1 : ID=0xd, active 0xcd, start-CHS (0x7\n2,19,7), end-CHS (0x388,254,6), startsector 2147916342, 243810273 sectors; parti\ntion 2 : ID=0x60, active 0x9c, start-CHS (0x366,6,3), end-CHS (0x3f7,49,18), sta\nrtsector 2298911798, 4154016209 sectors; partition 4 : ID=0xf8, active 0xe1, sta\nrt-CHS (0x2,184,1), end-CHS (0x313,6,13), startsector 3277940231, 0 sectors</code></pre>\n<p>If you take a closer look at the first line of the hex dump, you might\nbe able to spot the jump instruction that makes this little magic trick\npossible. Luckily, Phil Katz&#39; initials are valid opcodes. The Multiboot\nentry point is much further into the file at offset <strong>00000331</strong>.</p>\n<h2 id=\"zip-archives-revisited\">ZIP Archives Revisited</h2>\n<figure><img src=\"https://goliath32.com/img/blog/ziphdr.svg\" alt=\"\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></figure>\n<p>Here&#39;s a picture you probably recognize from\n<a href=\"https://en.wikipedia.org/wiki/ZIP_(file_format)\" rel=\"nofollow ugc noopener\">the Wikipedia article about the ZIP format</a>.\nThis here is a local copy, Wikimedia Commons hosts\n<a href=\"https://commons.wikimedia.org/wiki/File:ZIP-64_Internal_Layout.svg\" rel=\"nofollow ugc noopener\">the original with attribution and licensing information</a>.</p>\n<p>Much like a TAR or CPIO archive, a ZIP file is an amalgamation of tightly\npacked files with a short header in front, specifying the full file path\nand other meta data:</p>\n<pre><code>typedef struct {\n    uint32_t magic;\n    uint16_t version;\n    uint16_t flags;\n    uint16_t method;\n    uint16_t modtime;\n    uint16_t moddate;\n    uint32_t checksum;\n    uint32_t compsize;\n    uint32_t origsize;\n    uint16_t namelength;\n    uint16_t extralength;\n    char name[namelength];\n    uint8_t extra[extralength];\n} __attribute__((packed)) zip_entry_t;</code></pre>\n<p>The timestamps are DOS datetime, the checksum is a CRC-32, there are no\nattributes or permission bits, we are thoroughly in DOS land. Directories\nare implied by the file paths.</p>\n<p>Nit pick\nYes, I know, the central directory has a field where &quot;internal&quot; and\n&quot;external&quot; attributes can be stored, whose meaning depends on the OS ID\nbyte in the version field. Later versions of the format added an\nextension to support directories. Good luck making use of that in a\nportable way that can be understood by different archivers across\ndifferent WinDOS versions and Unix clones.</p>\n<p>Unlike TAR or CPIO, there is a <strong>central directory</strong> in the end, with\na listing of all of the file headers and back references to the file in\nthe archive. Unlike traditional file formats that have are\n<strong>started by a header</strong>, ZIP is <strong>terminated by a footer</strong>.</p>\n<p>The reason for this is twofold:</p>\n<ul><li><p>the archive can be generated in a single, forward pass (collecting</p><p>the directory in memory).</p></li><li><p>Accessing a single file in the archive can be done quickly, by</p><p>looking through the directory and going directly to the file.</p></li></ul>\n<p>That last part was especially important in the days when ZIP archives\nwould be split over several floppies. To access a file, just scan the\ndirectory on the last floppy, then tell the user what floppy to insert to\nget the file.</p>\n<p>By comparison, accessing a file in a tarball requires linearly scanning\nover the whole thing (which, on the other hand, is\n<a href=\"https://en.wikipedia.org/wiki/Tape_drive#Technical_limitations\" rel=\"nofollow ugc noopener\">preferable for tape drives</a>, where TAR originates).</p>\n<h3 id=\"polyglot-trickery\">Polyglot Trickery</h3>\n<p>ZIP archives are meant to be read from the back, the footer points to the\ncentral directory, the directory points to the files. <em>In theory</em>,\nthe archiver shouldn&#39;t care if there happens to be a gap between two\nfiles, containing something else or if there is something else in the\nfront of the archive, like a file header for a different file format.</p>\n<p>The local file headers also supports up to 64k of &quot;extra&quot; stuff between\nthe header and the file data, the footer has &quot;comment&quot; at the end, also\nup to 64k.</p>\n<p>If you look at the footer for a second, you might spot an interesting\ndesign flaw:</p>\n<pre><code>typedef struct {\n    uint32_t magic;\n    uint16_t disknum;\n    uint16_t diskstart;\n    uint16_t num_dir_entries;\n    uint16_t total_dir_entries;\n    uint32_t dir_size;\n    uint32_t dir_start;\n    uint16_t comment_length;\n    uint8_t comment[comment_length];\n} __attribute__((packed)) zip_end_t;</code></pre>\n<p>it is variable size, an archiver needs to find it looking\nfrom the back, but the magic and the size are both stored <em>before</em>\nthe variable sized comment field. Ask yourself this for a moment: how\nwould you implement this in an archiver? Demanding that there is no\ncomment? Reading the last 64k plus some change and scanning it? From\nwhich direction? What if there is another valid footer inside the\ncomment field of the footer? Depending on how an implementation deals\nwith this, a specifically crafted archive can appear to contain\ndifferent files when viewed with different archivers.</p>\n<p>Trying to make the format extensible ended up building a whole lot of\nfoot guns in there. And we are not even talking about the plethora of\nextensions that have been tacked on to the official specification over\nthe years.</p>\n<p>Many modern implementations decided to save themselves a lot of headaches\nand just treat ZIP archives the same way as tarballs, linearly scanning\nthem from the front instead. They expect the archive to start with a\nlocal header, skipping by the size <strong>MUST</strong> lead to the next entry\nor finally, the central directory. They reject any archives where this\nis not possible. This effectively prevents a lot of polyglot trick\nfuckery, but at the cost of rendering possibly valid archives unreadable.</p>\n<p>For the self booting ZIP archive, the challenge thus becomes: the archive\n<strong>MUST</strong> start with a local file header, there cannot be <em>any</em>\ngaps, everything must be properly contained inside files.</p>\n<h2 id=\"the-easy-part-multiboot\">The easy part: Multiboot</h2>\n<p>Anybody who has ever looked at a DIY toy OS tutorial has probably\nstumbled across\n<a href=\"https://www.gnu.org/software/grub/manual/multiboot/multiboot.html\" rel=\"nofollow ugc noopener\">The Multiboot Specification</a>, the possibly easiest and most beginner friendly way to get\na program running on bare metal x86.</p>\n<p>The multiboot spec originates in the GNU/FSF ecosystem and is implemented\nby boot loaders like GRUB or Qemu direct kernel boot. SYSLINUX supports\nit via a plugin. There is a draft for an over engineered 2nd version, that\nalso supports MIPS, but this is of no concert to us here.</p>\n<p>A Multiboot 1 compliant kernel simply contains the magic\nvalue <strong>0x1BADB002</strong> <em>somewhere</em> in the first in the first 8k.\nThis is followed by flags and a checksum (all 3 must add up to zero).\nDepending on the flags, additional fields may follow.</p>\n<p>The kernel itself can be an ELF file from which the loader extracts\nload address and entry point. The kernel is run in 32 bit protected\nmode with flat segments (base=0, limit=4G) and the A20 gate enabled.</p>\n<p>Nit pick\nThe specification actually doesn&#39;t mandate ELF, but rather &quot;an ordinary\n32-bit executable file in whatever file format the operating system\nnormally uses.&quot;, it then goes on to specify <strong>optional</strong> fields in\nthe info struct for either <strong>ELF</strong> or <strong>a.out</strong>. In theory, it\nshould also be possible for a boot loader to only support <strong>PE</strong> and\nnot be in violation of the spec.</p>\n<p>Alternatively to a plain old ELF file, the kernel can also specify the\nmemory layout manually (indicated via a flag), allowing flat binaries or\nother file formats to be loaded. The extended header looks like this:</p>\n<pre><code>typedef struct {\n    uint32_t magic;\n    uint32_t flags;\n    uint32_t checksum;\n    uint32_t hdr_addr;\n    uint32_t load_addr;\n    uint32_t load_end_addr;\n    uint32_t bss_end_addr;\n    uint32_t entry_point;\n} mb_header_t;</code></pre>\n<p>The <strong>hdr_addr</strong> specifies the in-memory address of the header after\nloading the file, using this and the file offset where the header was\nfound, the other addresses can be converted back to file offsets by the\nloader.</p>\n<p>This way, the <strong>load_addr</strong> and <strong>load_end_addr</strong> are used to figure\nout <em>what</em> parts of the file to load <em>where</em> into memory. If the\n<strong>bss_end_addr</strong> is past that, the loader zero-fills it, before finally\njumping to <strong>entry_point</strong> to run the kernel.</p>\n<p>The Multiboot compliant loader also sets up a structure with system\ninformation (e.g. memory map, APM tables), kernel command line,\netc… and passes the pointer to the kernel (via register\n<strong>EBX</strong>, magic value <strong>0x2BADB002</strong> in <strong>EAX</strong>).</p>\n<p>Making a Multiboot bootable ZIP archive is trivial. Simply pack a flat\nbinary with a Multiboot header as uncompressed file binary, make sure it\nis at the beginning and the addresses line up. An external tool can then\nfix up the headers, so that the whole archive is loaded.</p>\n<p>For any <em>practical</em> applications, it would make <em>sense</em> to stop\nright here, but we&#39;re not here for &quot;practical&quot; or &quot;sane&quot;.</p>\n<h2 id=\"making-it-bios-bootable\">Making it BIOS bootable</h2>\n<p>On an IBM compatible machine, the BIOS iterates over the installed\ndrives, loads the first sector from each (to location <strong>0x7c00</strong>),\nuntil it finds one where the first sector ends with <strong>0xaa55</strong>.\nIt then jumps to the first byte.</p>\n<p>Disks that are formatted with a filesystem, typically have a header in\nthe beginning that starts with a jump instruction across the header into\nthe bootstrap code that follows. The DOS MBR format opts to instead store\nthe partition table at the end of the first sector and all the code in\nthe front.</p>\n<p>If we stuff a BIOS boot signature into the ZIP archive, at least parts\nof the ZIP local file header will be executed, the first few bytes are\na mandated magic value and we cannot just put a jump instruction there.</p>\n<p>The puzzle exercise now becomes, filling the header fields with values\nthat make sense but also happen to be valid 8086 opcodes. At least,\nuntil we hit something that can be abused as a free-form field that we\ncan stuff a jump into.</p>\n<p>Let&#39;s take a look at that hex dump again:</p>\n<pre><code>00000000: 50 4b 03 04 0a 00 02 80 00 00 eb 44 e4 2e 41 0f  PK.........D..A.\n00000010: 01 8f 0a 09 00 00 0a 09 00 00 10 00 00 00 62 6f  ..............bo\n00000020: 6f 74 2f 5a 49 50 48 45 41 44 2e 43 4f 4d 00 90  ot/ZIPHEAD.COM..</code></pre>\n<p>I&#39;ve tried to highlight the relevant header up to the jump\ninstruction: <strong>0xeb</strong> is a relative jump with a single byte\nimmediate argument.</p>\n<p>If we take a look again at the header structure, we can make out what\nfields those bytes correspond to:</p>\n<pre><code>typedef struct {\n    uint32_t magic;\n    uint16_t version;\n    uint16_t flags;\n    uint16_t method;\n    uint16_t modtime;\n    ………</code></pre>\n<p>The timestamp is the first best place where we can put a jump\ninstruction. The <strong>0xeb</strong> relative jump works out to <strong>8:30-ish</strong>\nin the morning, give or take the jump distance.</p>\n<p>The two bytes in front of the <strong>modtime</strong> are compression\n<strong>method</strong>. We really, really want to have <strong>0x0000</strong> here,\ni.e. <em>uncompressed</em>, so that an archiver doesn&#39;t choke on the\nfile data.</p>\n<p>The first four bytes are the magic value of the format, we don&#39;t have any\nchoice what to put here either, it has to be &quot;<strong>PK\\03\\04</strong>&quot;. The\n<strong>version</strong> and <strong>flags</strong> fields are the only places where we have\nsome creative freedom.</p>\n<p>If we run the file through a dissassembler and take a look at the\nfirst few bytes, we&#39;ll end up with this:</p>\n<pre><code>$ objdump -D -m i8086 -b binary ./boot.zip\n\n./boot.zip:     file format binary\n\nDisassembly of section .data:\n\n00000000 &lt;.data&gt;:\n       0:       50                      push   %ax\n       1:       4b                      dec    %bx\n       2:       03 04                   add    (%si),%ax\n       4:       0a 00                   or     (%bx,%si),%al\n       6:       02 80 00 00             add    0x0(%bx,%si),%al\n       a:       eb 44                   jmp    0x50</code></pre>\n<p>This is AT&amp;T syntax, source operands are left, destination\non the right. We use GNU tools in this household, deal with it.</p>\n<p>Anything that just fiddles around with a register doesn&#39;t really hurt us.\nSame goes for memory <em>reads</em>. We don&#39;t have memory protection, no\nmatter what we access, there won&#39;t be any faults, the only memory\nmapped hardware around should be the frame buffer.</p>\n<p>Memory writes are another story. Worst case, we could shoot up the\ninterrupt vectors, the BIOS RAM areas (BDA, EBDA), or shadowed option\nROMs.</p>\n<p>The <strong>P** for Phil also happens to also decode to\n**push %ax</strong>. If the BIOS leaves us with a somewhat sane stack\npointer, the <strong>push</strong> instruction should hopefully not mess anything\nup. It is part of the ZIP magic and thus unavoidable.</p>\n<p>The <strong>version</strong> field is itself actually <em>structured</em>, containing\na decimal encoded version number, followed by an OS ID byte. If we set\nit to <strong>0x000a</strong> (stored as little endian), this decodes to version\n<strong>1.0</strong> on <strong>MS DOS</strong>.</p>\n<p><strong>0x0a</strong> is an <strong>orb</strong>, version 1.1 would be double byte sized,\nversion 0.8 and 0.9 would be register direct and not touch the memory.\nAn address of <strong>BX + SI</strong> is the result of a <strong>mod-reg-r/m</strong> field\nwith all zeros.</p>\n<p>We could try to code golf our way into consuming at least part of the\n<strong>flags</strong> as well, but low-valued, &quot;harmless&quot; opcodes that result\nin sane versions are sparse. Also, some version numbers change the\nmeaning how an archiver is supposed to interpret the file. Also also,\n<strong>mod-reg-r/m</strong> combinations in the OS code field quickly end up as\nan undefined OS ID. Also also also, we have the <strong>method</strong> as well,\nthat <em>has to be</em> <strong>0x0000</strong>, which would result in a memory\nwrite if executed. We have to consume that somehow, rather than running\nit.</p>\n<p>So for the <strong>flags</strong>, I came up with <strong>0x8002</strong> (stored as little\nendian). The lowest, set bit has a compression method dependent meaning,\ncurrently only defined for LZMA (14), deflate (8) and implode (6). For\nuncompressed (method 0) there is no defined meaning (the specification\njust ignores that part, it doesn&#39;t use nasty words like\n<em>undefined</em>). For deflate it would mean that\n<em>maximum compression</em> was used.</p>\n<p>The MSB is specified as &quot;reserved for use by PKWARE&quot;, whatever they chose\nto put there, if they ever chose anything at all. Most imporanly, the\nopcode <strong>0x02</strong> decodes to a single byte, indirect add, and in the\n<strong>mod-reg-r/m</strong>, the MSB tells the CPU to consume another 2 bytes of\nimmediate <strong>DISP</strong>lacement. This is how we can get rid of the\ncompression method.</p>\n<p>At this point, we&#39;ve made it! We reached the <strong>modtime</strong> field\nthat we can just stuff a jump into.</p>\n<h2 id=\"drawing-the-rest-of-the-owl-multiboot-on-foot\">Drawing the rest of the owl: Multiboot on foot</h2>\n<p>Once we&#39;ve made it past the header, we are in 16 bit real mode. We just\nneed to go through all of the neccessary steps to set up the environment\nthat the Multiboot code expects:</p>\n<ul><li>Initialization (setup segments, clear scratch memory)</li><li>Figure out what kind of CPU we are running on</li><li><p>We need <em>at least</em> an i386, so we</p><p>can <em>actually run</em> 32 bit code</p></li><li><p>The <strong>CPUID</strong> instruction was added to the Pentium and later 486,</p><p>so we can&#39;t use that</p></li><li><p>We need to tickle undefined bits in the <strong>FLAGS</strong> register</p><p>and watch what happens</p></li><li><p>Query disk parameters, load additional boot code past the</p><p>first sector</p>\n<ul><li>We need the C/H/S geometry to sanely read past the first track</li><li><p>For simplicity, work with LBA and convert to C/H/S bit confetti for</p><p><strong>INT 13h</strong> as needed</p></li></ul></li><li>Detect available memory<ul><li>Memory below 1M via <strong>INT 12h</strong></li><li><p>If supported, high memory via <strong>INT 15h</strong>, <strong>AX=E801h</strong> to</p><p>query memory before and after the (possible) ISA memory hole\nat 15M</p></li><li><p>If that fails, fallback to <strong>INT 15h</strong>, <strong>AH=88h</strong> to</p><p>get a lump sum of available high memory</p></li><li><p>If supported, get a detailed memory map</p><p>via <strong>INT 15h</strong>, <strong>AX=E820h</strong>. This already uses the format\nspecified by the Multiboot spec, just slap an extra size value in\nfront of every entry.</p></li></ul></li><li>Turn on the A20 gate<ul><li><p>Check if memory location <strong>0000:0600</strong> (=0x0600) and</p><p><strong>ffff:0610</strong> (=0x100600) overlap or not</p></li><li>Try BIOS <strong>INT 15h</strong>, <strong>AX=2401h</strong></li><li>If that fails, try talking to the keyboard controller directly</li><li>If that fails, try SYSCTL A register</li></ul></li><li>Load a GDT and set up 16 bit unreal mode<ul><li><p>To <em>actually access</em> high memory, we need to</p><p>get rid of the 64k segment limits</p></li><li><p>We need to stay in <em>real-ish</em> mode, so we can</p><p>keep using <strong>INT 13h</strong></p></li></ul></li><li>Load the rest of the archive into high memory</li><li>Jump to the Multiboot entry point<ul><li>Re-enter protected mode, 32 bit code segment</li><li>Cobble the rest of the Multiboot info struct together</li><li>Leap-of-faith into highmem</li></ul></li></ul>\n<p>This might sound like a lot, but in the current version, the actual code\nboils down to 817 bytes. That includes all the headers (ZIP, multiboot),\nthe rather bulky GDT. But it is still quite a lot and outside the scope\nof this post anyway, I might pick some interesting parts out of it for\nsome follow-up posts (watch this space?).</p>\n<h2 id=\"through-the-looking-glass\">Through the Looking Glass</h2>\n<p>The Multiboot entry code re-loads the GDT, but this time using the\nhighmem location, and sets up a stack after the archive. The size is\nextracted from the Multiboot header. Knowing the start address, the C\ncode can then iterate over itself to locate and access files in the\narchive.</p>\n<p>Some linker script trickery is used to tie it all into one blob, but with\nthe GDT and Multiboot header visible from both lomem and highmem code at\ndifferent locations.</p>\n<p>At this point, you might be inclined to actually read the source code\nin the ZIP archive. There are lots of comments trying to explain what\nis going on.</p>\n<p>There&#39;s a README that points out the structure of the code with\nsuggestions where to start reading. I decided to keep it readable rather\nthan stuff it all into a single sector. Self-modifying trickery is kept\nto a minimum (to safe some space, the function to enter unreal mode\nmodifies itself, the code is run twice, courtesy of a misdirected\n<strong>ret</strong>, entering protected mode the first time round, leaving it\nagain and restoring the segment registers on the second run, before\nreturning for real).</p>\n<p>Code in the archive and this blog posts have been written with\nassistance from:</p>\n<ul><li>Tegernseer Hell</li><li><p><a href=\"https://www.youtube.com/watch?v=NEivFBrGESk\" rel=\"nofollow ugc noopener\">Gouryella</a></p><p>et al</p></li></ul>\n<p>If you poke around in the code and have any suggestions for improvements,\nor otherwise do anything interesting with it, please let me know.</p>\n<p>Have fun!</p>","headings":[{"level":1,"text":"Creating a self booting ZIP archive","id":"creating-a-self-booting-zip-archive"},{"level":2,"text":"Introduction","id":"introduction"},{"level":2,"text":"ZIP Archives Revisited","id":"zip-archives-revisited"},{"level":3,"text":"Polyglot Trickery","id":"polyglot-trickery"},{"level":2,"text":"The easy part: Multiboot","id":"the-easy-part-multiboot"},{"level":2,"text":"Making it BIOS bootable","id":"making-it-bios-bootable"},{"level":2,"text":"Drawing the rest of the owl: Multiboot on foot","id":"drawing-the-rest-of-the-owl-multiboot-on-foot"},{"level":2,"text":"Through the Looking Glass","id":"through-the-looking-glass"}]}}