Sam Trenholme's webpage
This article was posted to the Usenet group alt.hackers in 1995; any technical information is probably outdated.

Boostrap code (Was: Re: Hacker FAQ)


Article: 7706 of alt.hackers
From: Terje.Mathisen@hda.hydro.com (Terje Mathisen)
Newsgroups: alt.hackers
Subject: Boostrap code (Was: Re: Hacker FAQ)
Date: 23 Apr 1995 13:45:23 GMT
Organization: Hydro Data, Norsk Hydro (Norway)
Lines: 112
Approved: Yes.
Message-ID: 3ndllj$p9s@vkhdsu01.hda.hydro.com
Reply-To: Terje.Mathisen@hda.hydro.com (Terje Mathisen)
NNTP-Posting-Host: sk08ts.hda.hydro.com
X-Newsreader: IBM NewsReader/2 v1.02
Status: RO

In <grobsonD7EqnE.95q@netcom.com>, grobson@netcom.com (Gary D. Robson)
writes:
>ObHackFromThePast:
>
>When I first started working in the minicomputer world, we were using
>7-track tapes.  I was doing an operating system for a 16-bit machine.
>The way the system worked was that you'd mount the tape, press the
>"reset" switch on the console, and it would read the first
sector of the
>boot device (in this case, the tape drive).
>
>This meant that I had to create a driver for the 7-track reel-to-reel
>tape drive that fit in a single sector, and that driver needed to load
>the bootstrap program from the following sector(s) of the tape.  The
>problem was, I could only use opcodes containing zeros in bits 0, 1, 8,
>and 9.  It turned out to be possible to make a little routine just a few
>instructions long that "decoded" the rest of the sector and
then turned
>over control to it.

That sounds very similar (in principle) to something I wrote a few weeks
ago, based on an idea in an article by Jim Lawless (W/DDJ april issue?).

The idea is to use a 7-bit ascii mail system to send binary files to
somebody who don't have uudecode or similar programs available, by encoding
a short bootstrap program as hex codes, use debug to decode that, pass
control to the bootstrap stub, and let that decode the remainder of the input.

Jim had used a uuencode-like system, which uses 6 bits from each of four
characters to send 3 binary bytes.  Using a state machine, his decoder
needed 118 bytes.

I got the decoder down to 55 bytes, and switched to a modulo-85 encoding,
which lets me encode 4 bytes in 5 characters.

The encoder is capable of handling executable files (which debug.exe
cannot save back to disk), and files larger than 64k, both by generating
one or more temp files, each slightly less than 64k, and then making a
final block, which encodes a batch file, dbtemp.bat (generated on the fly
while encoding), that will do the neccessary:

 copy /b $.$0 + $.$1 + $.$2 [+...] filename.exe

command to regenerate the input file.

The decoder stub is using quite a few (semi-)dirty tricks to reduce the size,
the best is probably that I load the stub in the last 55 bytes in the segment,
put the stack pointer below that, and then use the stack as my output
buffer, saving each newly-decoded 32-bit word (in BP:AX) with two simple
PUSH operations (using one byte each!).

To exit the stub program (on finding an empty input line), I use INT 3
instead of INT 20, since INT 3 is a single-byte opcode (0CCh).  This
will always work since I'm running under debug!

-Terje Mathisen (include std disclaimer) <Terje.Mathisen@hda.hydro.com>
"almost all programming can be viewed as an exercise in caching"

Below, I've included the source for the encoder (dbencode.c), and
the stub decoder (asm), in a dbencode'd text file. Extract the remainder,
save it as a temp file and redirect that file into debug to generate
dbencode.zip!

eFFC9 33 C0 33 ED B3 5 46 95 F7 E7 95 F7 E7 B9 DF 0 2 C
eFFDB 3 C1 13 EA FE CB 75 EC 55 50 80 EF 5 77 DF B4 A BE
eFFED 5C 0 8B D6 88 14 46 CD 21 8A 3C BF 55 0 84 FF 75 CA CC
rsp
920
g=ffea
*g6R(!!!7i!!!">!!3-%!!!!!"pIXr9iF>1779p=779I1!!!0f!!!!A!!*'"!!!!!!"8i-
"bQdT!\@"V%FuSaC=<.g!!!!5!#,J892Iu.779p=:.7K;!!!!!!!!"l!!*'"!!!!!!"&]+
%(ZE="W0jHCq1UNCaK,f!!!!5!#,J892G]h+9.pf4/`;#=L>;R>#:I*bgUeN-Bu6,AGA5)
?lhq,=F',`4%*6GpWegTa/hEg=@^5mqG!8;Bf8OQF+Rh?IVdr0R"\8(-lf^jB^h)L3!Q@e
-G1)FU3j>G]:M\f"[$ZU;WjV9pL@)tH8G,C'aZ"rEo*9U.LhkX=i-=QIC`;.Z(q2$\='Um
<Pc9Z^ro9,ZuX6hf3/j,V1?a3S`'iM7ef%X@!Ln*XLt_SBC2k/&,M:p7]\b5"LNH]_dp(i
I_a,/k3etp<KMcV2(LTV<2gP]T>O(_YN4s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