Creating a self booting ZIP archive
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Creating a self booting ZIP archive
Creating a self booting ZIP archive Back to the Blog Index Introduction So, after a debugging session the other day, I was having some shower thoughts regarding early userspace and initrd. Linux initramfs supports unpacking CPIO, a stupid, simple, standardized format. In the discussion on whether to support TAR instead, much emphasize has been put on the fact that CPIO is standardized and less fragmented. Luckily, Linux (the kernel) includes its own tool to produce the specific variant of the standard that it happens to implement. The standard CPIO tool is possibly the most cursed archiver under the SUN.
创建自启动 ZIP 压缩包。回到博客索引。引言:前几天在一次调试会话后,我产生了一些关于早期用户空间和 initrd 的想法。Linux initramfs 支持解压 CPIO,这是一种简单、标准化的格式。在讨论是否支持 TAR 时,人们强调了 CPIO 标准化程度更高且碎片化更少。幸运的是,Linux 内核自带了工具来生成它所实现的特定标准变体。标准的 CPIO 工具可能是太阳底下最糟糕的归档程序。
In the old DOS/WinDOS world, ZIP was probably the most wide spread and well known archive format, being displaced to some extend by the rise of RAR during the 2000s. Because of the design of the format, ZIP lends itself to all kinds of polyglot trickery. Way back when, self extracting ZIP archives where a thing, EXE/ZIP polyglots that can be run and unpack themselves, which is useful if you don’t have an archiver installed. I don’t think I need to explain where I am going with this, here is the resulting ZIP archive I built: Download boot.zip
在旧的 DOS/WinDOS 世界中,ZIP 可能是最广泛且最知名的归档格式,在 2000 年代随着 RAR 的兴起而在一定程度上被取代。由于其格式设计,ZIP 非常适合各种多语言(polyglot)技巧。很久以前,自解压 ZIP 压缩包很流行,即可以运行并自行解压的 EXE/ZIP 多语言文件,这在没有安装归档程序时非常有用。我想我不需要解释我的意图了,这是我构建的 ZIP 压缩包:下载 boot.zip。
It is a completely normal ZIP archive, containing its own source code needed to build itself from scratch: $ unzip -t boot.zip Archive: boot.zip testing: boot/ZIPHEAD.COM OK testing: boot/MESSAGE.TXT OK testing: common/asm.h OK testing: common/multiboot.h OK testing: common/zip.h OK testing: multiboot/console.c OK testing: multiboot/console.h OK testing: multiboot/multiboot.c OK testing: multiboot/string.S OK testing: setup/a20.S OK testing: setup/disk.S OK testing: setup/entry16.S OK testing: setup/entry32.S OK testing: setup/mbload.S OK testing: setup/mem.S OK testing: setup/setup.ld OK testing: util/fixhdr.c OK testing: util/zipglue.c OK testing: util/zippack.c OK testing: mk.sh OK testing: README.txt OK testing: MESSAGE.txt OK testing: LICENSE.txt OK No errors detected in compressed data of boot.zip.
这是一个完全正常的 ZIP 压缩包,包含了从零开始构建它所需的源代码:$ unzip -t boot.zip(此处省略测试输出列表)。boot.zip 的压缩数据中未检测到错误。
A particularly interesting file here is boot/ZIPHEAD.COM, that contains executable code that is run when booting the archive. You need to fix the execute permissions of mk.sh after unpacking, and you will need a copy of Zlib installed for the tools. Running mk.sh from it’s directory should leave you with a fresh copy of the bootable ZIP archive. The simplest way to run it, is to just pass it to Qemu direct kernel boot: $ qemu-system-i386 -kernel boot.zip
其中一个特别有趣的文件是 boot/ZIPHEAD.COM,它包含了引导压缩包时运行的可执行代码。解压后,你需要修复 mk.sh 的执行权限,并且需要安装 Zlib 副本以供工具使用。在 mk.sh 所在目录运行它,应该会生成一个新的可引导 ZIP 压缩包。运行它的最简单方法是将其直接传递给 Qemu 内核引导:$ qemu-system-i386 -kernel boot.zip
“That’s easy” you might say, “Qemu supports Multiboot, that ZIPHEAD.COM is uncompressed, at the beginning of the archive and has a Multiboot header in it!” Yes, that’s all true! But this ZIP archive also brings it’s own boot loader along. You can try putting it on a floppy and throw that in front of Qemu: $ dd if=/dev/zero of=floppy.img count=2880 $ dd if=boot.zip of=floppy.img conv=notrunc $ qemu-system-i386 -fda floppy.img
你可能会说:“这很简单,Qemu 支持 Multiboot,那个 ZIPHEAD.COM 是未压缩的,位于压缩包开头,并且里面有一个 Multiboot 头!”是的,这些都是真的!但这个 ZIP 压缩包还自带了引导加载程序。你可以尝试把它放在软盘上,然后扔给 Qemu:$ dd if=/dev/zero of=floppy.img count=2880 $ dd if=boot.zip of=floppy.img conv=notrunc $ qemu-system-i386 -fda floppy.img
The first 512 bytes of the archive contain a valid boot sector that loads some more code from the second sector and then bootstraps itself into highmem with the A20 enabled and 32 bit protected mode set up. You can also put it on a hard drive too, it detects what INT 13h drive it’s been loaded from and dutifully queries the disk geometry. If you care to try, you can also dump it on a real floppy and stuff it into a PC: That’s a Pentium MMX by the way, but I also tried an i486 DX2 and an Am386DX-40, just to be sure. My 486 had me hold my breath for a moment, it has a spectacularly slow floppy drive. The text on screen is from the file boot/MESSAGE.TXT
压缩包的前 512 字节包含一个有效的引导扇区,它从第二个扇区加载更多代码,然后在启用 A20 和设置 32 位保护模式的情况下将自身引导至高端内存。你也可以把它放在硬盘上,它会检测是从哪个 INT 13h 驱动器加载的,并忠实地查询磁盘几何结构。如果你有兴趣尝试,也可以把它转储到真实的软盘上并塞进 PC:顺便说一下,那是 Pentium MMX,但我为了保险起见也尝试了 i486 DX2 和 Am386DX-40。我的 486 让我屏住呼吸了一会儿,它的软驱慢得惊人。屏幕上的文字来自文件 boot/MESSAGE.TXT。
“But that’s not special either” you might respond, “ZIPs are read from the back, you can just put anything you want in front, so you put a boot sector there!” That is sadly not quite true anymore, I’ll get to that. But for now, lets take a look at the first couple bytes of that archive together and we’ll see the real party trick: $ xxd -g 1 -l 128 boot.zip (hex dump omitted)
你可能会回应:“但这也没什么特别的,ZIP 是从后面读取的,你可以在前面放任何你想放的东西,所以你放了一个引导扇区在那里!”遗憾的是,这已经不完全正确了,我稍后会讲到。但现在,让我们一起看看该压缩包的前几个字节,看看真正的绝活:$ xxd -g 1 -l 128 boot.zip(十六进制转储省略)。
The ASCII P stands for Phil ( not Picard ), as in Phil Katz, followed by 03h and 04h. What you are looking at, is a completely valid ZIP local file header for boot/ZIPHEAD.COM Oh, and those other bytes there after the ZIP header: 02 b0 ad 1b, if you flip them around to human endian, it reads 1BADB002. That’s the Multiboot magic :-) The 16 bit real mode code starts right after the Multiboot header, at offset 00000050. If you are feeling a little confused, so is the file utility: $ file boot.zip (file utility output omitted)
ASCII 码中的 P 代表 Phil(不是 Picard),即 Phil Katz,后面跟着 03h 和 04h。你所看到的是 boot/ZIPHEAD.COM 一个完全有效的 ZIP 本地文件头。哦,还有 ZIP 头后面的那些字节:02 b0 ad 1b,如果你把它们翻转成人类可读的字节序,它读作 1BADB002。这就是 Multiboot 的魔力 :-) 16 位实模式代码就在 Multiboot 头之后,偏移量 00000050 处开始。如果你感到有点困惑,file 工具也是如此:$ file boot.zip(file 工具输出省略)。
If you take a closer look at the first line of the hex dump, you might be able to spot the jump instruction that makes this little magic trick possible. Luckily, Phil Katz’ initials are valid opcodes. The Multiboot entry point is much further into the file at offset 00000331. ZIP Archives Revisited Here’s a picture you probably recognize from the Wikipedia article about the ZIP format. This here is a local copy, Wikimedia Commons hosts the original with attribution and licensing information. Much like a TAR or CPIO archive, a ZIP file is an amalgamation of tightly packed files with a short header in front, sp
如果你仔细观察十六进制转储的第一行,你可能会发现使这个小魔术成为可能的跳转指令。幸运的是,Phil Katz 的首字母缩写是有效的操作码。Multiboot 入口点在文件更靠后的偏移量 00000331 处。重温 ZIP 压缩包:这是一张你可能从维基百科关于 ZIP 格式的文章中认出的图片。这是本地副本,维基共享资源托管了带有署名和许可信息的原始图片。就像 TAR 或 CPIO 归档一样,ZIP 文件是紧密打包文件的集合,前面有一个短头……