2 * Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
4 * This file is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * Originally this code was part of ucl the data compression library
10 * for upx the ``Ultimate Packer of eXecutables''.
12 * - Converted to gas assembly, and refitted to work with etherboot.
13 * Eric Biederman 20 Aug 2002
15 * - Structure modified to be a subroutine call rather than an
17 * Michael Brown 30 Mar 2004
19 * - Modified to be compilable as either 16-bit or 32-bit code.
20 * Michael Brown 9 Mar 2005
23 /****************************************************************************
24 * This file provides the decompress() and decompress16() functions
25 * which can be called in order to decompress an image compressed with
26 * the nrv2b utility in src/util.
28 * These functions are designed to be called by the prefix. They are
29 * position-independent code.
31 * The same basic assembly code is used to compile both
32 * decompress() and decompress16().
33 ****************************************************************************
38 .section ".prefix", "ax", @progbits
41 /****************************************************************************
42 * decompress16 (real-mode near call, position independent)
44 * Decompress data in 16-bit mode
46 * Parameters (passed via registers):
47 * %ds:%esi - Start of compressed input data
48 * %es:%edi - Start of output buffer
50 * %ds:%esi - End of compressed input data
51 * %es:%edi - End of decompressed output data
52 * All other registers are preserved
54 * NOTE: It would be possible to build a smaller version of the
55 * decompression code for -DKEEP_IT_REAL by using
57 * to use 16-bit registers where possible. This would impose limits
58 * that the compressed data size must be in the range [1,65533-%si]
59 * and the uncompressed data size must be in the range [1,65536-%di]
60 * (where %si and %di are the input values for those registers). Note
61 * particularly that the lower limit is 1, not 0, and that the upper
62 * limit on the input (compressed) data really is 65533, since the
63 * algorithm may read up to three bytes beyond the end of the input
64 * data, since it reads dwords.
65 ****************************************************************************
76 /****************************************************************************
77 * decompress (32-bit protected-mode near call, position independent)
79 * Parameters (passed via registers):
80 * %ds:%esi - Start of compressed input data
81 * %es:%edi - Start of output buffer
83 * %ds:%esi - End of compressed input data
84 * %es:%edi - End of decompressed output data
85 * All other registers are preserved
86 ****************************************************************************
108 /* Do the decompression */
111 dec %xBP /* last_m_off = -1 */
112 add $4, %xSI /* Skip "file length" field */
115 decompr_literals_n2b:
123 jc decompr_literals_n2b
125 inc %xAX /* m_off = 1 */
128 adc %xAX, %xAX /* m_off = m_off*2 + getbit() */
130 jnc loop1_n2b /* while(!getbit()) */
132 jb decompr_ebpeax_n2b /* if (m_off == 2) goto decompr_ebpeax_n2b ? */
134 movb (%xSI), %al /* m_off = (m_off - 3)*256 + src[ilen++] */
137 jz decompr_end_n2b /* if (m_off == 0xffffffff) goto decomp_end_n2b */
138 mov %xAX, %xBP /* last_m_off = m_off ?*/
142 adc %xCX, %xCX /* m_len = getbit() */
144 adc %xCX, %xCX /* m_len = m_len*2 + getbit()) */
145 jnz decompr_got_mlen_n2b /* if (m_len == 0) goto decompr_got_mlen_n2b */
146 inc %xCX /* m_len++ */
149 adc %xCX, %xCX /* m_len = m_len*2 + getbit() */
151 jnc loop2_n2b /* while(!getbit()) */
153 inc %xCX /* m_len += 2 */
154 decompr_got_mlen_n2b:
156 adc $1, %xCX /* m_len = m_len + 1 + (last_m_off > 0xd00) */
158 lea (%xBP,%xDI), %xSI /* m_pos = dst + olen + -m_off */
160 es movsb /* dst[olen++] = *m_pos++ while(m_len > 0) */
170 sub $-4, %xSI /* sets carry flag */
176 /* Restore registers and return */