~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/io/xzio.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* xzio.c - decompression support for xz */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2010  Free Software Foundation, Inc.
 
5
 *
 
6
 *  GRUB is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation, either version 3 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  GRUB is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <grub/err.h>
 
21
#include <grub/mm.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/file.h>
 
24
#include <grub/fs.h>
 
25
#include <grub/dl.h>
 
26
 
 
27
#include "xz.h"
 
28
#include "xz_stream.h"
 
29
 
 
30
#define XZBUFSIZ 0x2000
 
31
#define VLI_MAX_DIGITS 9
 
32
#define XZ_STREAM_FOOTER_SIZE 12
 
33
 
 
34
struct grub_xzio
 
35
{
 
36
  grub_file_t file;
 
37
  struct xz_buf buf;
 
38
  struct xz_dec *dec;
 
39
  grub_uint8_t inbuf[XZBUFSIZ];
 
40
  grub_uint8_t outbuf[XZBUFSIZ];
 
41
  grub_off_t saved_offset;
 
42
};
 
43
 
 
44
typedef struct grub_xzio *grub_xzio_t;
 
45
static struct grub_fs grub_xzio_fs;
 
46
 
 
47
static grub_size_t
 
48
decode_vli (const grub_uint8_t buf[], grub_size_t size_max,
 
49
            grub_uint64_t * num)
 
50
{
 
51
  if (size_max == 0)
 
52
    return 0;
 
53
 
 
54
  if (size_max > VLI_MAX_DIGITS)
 
55
    size_max = VLI_MAX_DIGITS;
 
56
 
 
57
  *num = buf[0] & 0x7F;
 
58
  grub_size_t i = 0;
 
59
 
 
60
  while (buf[i++] & 0x80)
 
61
    {
 
62
      if (i >= size_max || buf[i] == 0x00)
 
63
        return 0;
 
64
 
 
65
      *num |= (uint64_t) (buf[i] & 0x7F) << (i * 7);
 
66
    }
 
67
 
 
68
  return i;
 
69
}
 
70
 
 
71
static grub_ssize_t
 
72
read_vli (grub_file_t file, grub_uint64_t * num)
 
73
{
 
74
  grub_uint8_t buf[VLI_MAX_DIGITS];
 
75
  grub_ssize_t read;
 
76
  grub_size_t dec;
 
77
 
 
78
  read = grub_file_read (file, buf, VLI_MAX_DIGITS);
 
79
  if (read < 0)
 
80
    return -1;
 
81
 
 
82
  dec = decode_vli (buf, read, num);
 
83
  grub_file_seek (file, file->offset - (read - dec));
 
84
  return dec;
 
85
}
 
86
 
 
87
/* Function xz_dec_run() should consume header and ask for more (XZ_OK)
 
88
 * else file is corrupted (or options not supported) or not xz.  */
 
89
static int
 
90
test_header (grub_file_t file)
 
91
{
 
92
  grub_xzio_t xzio = file->data;
 
93
  xzio->buf.in_size = grub_file_read (xzio->file, xzio->inbuf,
 
94
                                      STREAM_HEADER_SIZE);
 
95
 
 
96
  if (xzio->buf.in_size != STREAM_HEADER_SIZE)
 
97
    {
 
98
      grub_error (GRUB_ERR_BAD_FILE_TYPE, "no xz magic found");
 
99
      return 0;
 
100
    }
 
101
 
 
102
  enum xz_ret ret = xz_dec_run (xzio->dec, &xzio->buf);
 
103
 
 
104
  if (ret == XZ_FORMAT_ERROR)
 
105
    {
 
106
      grub_error (GRUB_ERR_BAD_FILE_TYPE, "no xz magic found");
 
107
      return 0;
 
108
    }
 
109
 
 
110
  if (ret != XZ_OK)
 
111
    {
 
112
      grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "not supported xz options");
 
113
      return 0;
 
114
    }
 
115
 
 
116
  return 1;
 
117
}
 
118
 
 
119
/* Try to find out size of uncompressed data,
 
120
 * also do some footer sanity checks.  */
 
121
static int
 
122
test_footer (grub_file_t file)
 
123
{
 
124
  grub_xzio_t xzio = file->data;
 
125
  grub_uint8_t footer[FOOTER_MAGIC_SIZE];
 
126
  grub_uint32_t backsize;
 
127
  grub_uint8_t imarker;
 
128
  grub_uint64_t uncompressed_size_total = 0;
 
129
  grub_uint64_t uncompressed_size;
 
130
  grub_uint64_t records;
 
131
 
 
132
  grub_file_seek (xzio->file, xzio->file->size - FOOTER_MAGIC_SIZE);
 
133
  if (grub_file_read (xzio->file, footer, FOOTER_MAGIC_SIZE) !=
 
134
      FOOTER_MAGIC_SIZE
 
135
      || grub_memcmp (footer, FOOTER_MAGIC, FOOTER_MAGIC_SIZE) != 0)
 
136
    goto ERROR;
 
137
 
 
138
  grub_file_seek (xzio->file, xzio->file->size - 8);
 
139
  if (grub_file_read (xzio->file, &backsize, sizeof (backsize))
 
140
      != sizeof (backsize))
 
141
    goto ERROR;
 
142
 
 
143
  /* Calculate real backward size.  */
 
144
  backsize = (grub_le_to_cpu32 (backsize) + 1) * 4;
 
145
 
 
146
  /* Set file to the beginning of stream index.  */
 
147
  grub_file_seek (xzio->file,
 
148
                  xzio->file->size - XZ_STREAM_FOOTER_SIZE - backsize);
 
149
 
 
150
  /* Test index marker.  */
 
151
  if (grub_file_read (xzio->file, &imarker, sizeof (imarker)) !=
 
152
      sizeof (imarker) && imarker != 0x00)
 
153
    goto ERROR;
 
154
 
 
155
  if (read_vli (xzio->file, &records) <= 0)
 
156
    goto ERROR;
 
157
 
 
158
  for (; records != 0; records--)
 
159
    {
 
160
      if (read_vli (xzio->file, &uncompressed_size) <= 0)       /* Ignore unpadded.  */
 
161
        goto ERROR;
 
162
      if (read_vli (xzio->file, &uncompressed_size) <= 0)       /* Uncompressed.  */
 
163
        goto ERROR;
 
164
 
 
165
      uncompressed_size_total += uncompressed_size;
 
166
    }
 
167
 
 
168
  file->size = uncompressed_size_total;
 
169
  grub_file_seek (xzio->file, STREAM_HEADER_SIZE);
 
170
  return 1;
 
171
 
 
172
ERROR:
 
173
  grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "bad footer magic");
 
174
  return 0;
 
175
}
 
176
 
 
177
static grub_file_t
 
178
grub_xzio_open (grub_file_t io)
 
179
{
 
180
  grub_file_t file;
 
181
  grub_xzio_t xzio;
 
182
 
 
183
  file = (grub_file_t) grub_zalloc (sizeof (*file));
 
184
  if (!file)
 
185
    return 0;
 
186
 
 
187
  xzio = grub_zalloc (sizeof (*xzio));
 
188
  if (!xzio)
 
189
    {
 
190
      grub_free (file);
 
191
      return 0;
 
192
    }
 
193
 
 
194
  xzio->file = io;
 
195
  xzio->saved_offset = 0;
 
196
 
 
197
  file->device = io->device;
 
198
  file->offset = 0;
 
199
  file->data = xzio;
 
200
  file->read_hook = 0;
 
201
  file->fs = &grub_xzio_fs;
 
202
  file->size = GRUB_FILE_SIZE_UNKNOWN;
 
203
  file->not_easly_seekable = 1;
 
204
 
 
205
  if (grub_file_tell (xzio->file) != 0)
 
206
    grub_file_seek (xzio->file, 0);
 
207
 
 
208
  /* Allocated 64KiB for dictionary.
 
209
   * Decoder will relocate if bigger is needed.  */
 
210
  xzio->dec = xz_dec_init (1 << 16);
 
211
  if (!xzio->dec)
 
212
    {
 
213
      grub_free (file);
 
214
      grub_free (xzio);
 
215
      return 0;
 
216
    }
 
217
 
 
218
  xzio->buf.in = xzio->inbuf;
 
219
  xzio->buf.in_pos = 0;
 
220
  xzio->buf.in_size = 0;
 
221
  xzio->buf.out = xzio->outbuf;
 
222
  xzio->buf.out_pos = 0;
 
223
  xzio->buf.out_size = XZBUFSIZ;
 
224
 
 
225
  if (!test_header (file) || !(grub_file_seekable (io) && test_footer (file)))
 
226
    {
 
227
      grub_errno = GRUB_ERR_NONE;
 
228
      grub_file_seek (io, 0);
 
229
      xz_dec_end (xzio->dec);
 
230
      grub_free (xzio);
 
231
      grub_free (file);
 
232
 
 
233
      return io;
 
234
    }
 
235
 
 
236
  return file;
 
237
}
 
238
 
 
239
static grub_ssize_t
 
240
grub_xzio_read (grub_file_t file, char *buf, grub_size_t len)
 
241
{
 
242
  grub_ssize_t ret = 0;
 
243
  grub_ssize_t readret;
 
244
  enum xz_ret xzret;
 
245
  grub_xzio_t xzio = file->data;
 
246
  grub_off_t current_offset;
 
247
 
 
248
  /* If seek backward need to reset decoder and start from beginning of file.
 
249
     TODO Possible improvement by jumping blocks.  */
 
250
  if (file->offset < xzio->saved_offset)
 
251
    {
 
252
      xz_dec_reset (xzio->dec);
 
253
      xzio->saved_offset = 0;
 
254
      xzio->buf.out_pos = 0;
 
255
      xzio->buf.in_pos = 0;
 
256
      xzio->buf.in_size = 0;
 
257
      grub_file_seek (xzio->file, 0);
 
258
    }
 
259
 
 
260
  current_offset = xzio->saved_offset;
 
261
 
 
262
  while (len > 0)
 
263
    {
 
264
      xzio->buf.out_size = grub_min (file->offset + ret + len - current_offset,
 
265
                                     XZBUFSIZ);
 
266
 
 
267
      /* Feed input.  */
 
268
      if (xzio->buf.in_pos == xzio->buf.in_size)
 
269
        {
 
270
          readret = grub_file_read (xzio->file, xzio->inbuf, XZBUFSIZ);
 
271
          if (readret < 0)
 
272
            return -1;
 
273
          xzio->buf.in_size = readret;
 
274
          xzio->buf.in_pos = 0;
 
275
        }
 
276
 
 
277
      xzret = xz_dec_run (xzio->dec, &xzio->buf);
 
278
      switch (xzret)
 
279
        {
 
280
        case XZ_MEMLIMIT_ERROR:
 
281
        case XZ_FORMAT_ERROR:
 
282
        case XZ_OPTIONS_ERROR:
 
283
        case XZ_DATA_ERROR:
 
284
        case XZ_BUF_ERROR:
 
285
          grub_error (GRUB_ERR_BAD_COMPRESSED_DATA,
 
286
                      "file corrupted or unsupported block options");
 
287
          return -1;
 
288
        default:
 
289
          break;
 
290
        }
 
291
 
 
292
      {
 
293
        grub_off_t new_offset = current_offset + xzio->buf.out_pos;
 
294
        
 
295
        if (file->offset <= new_offset)
 
296
          /* Store first chunk of data in buffer.  */
 
297
          {
 
298
            grub_size_t delta = new_offset - (file->offset + ret);
 
299
            grub_memmove (buf, xzio->buf.out + (xzio->buf.out_pos - delta),
 
300
                          delta);
 
301
            len -= delta;
 
302
            buf += delta;
 
303
            ret += delta;
 
304
          }
 
305
        current_offset = new_offset;
 
306
      }
 
307
      xzio->buf.out_pos = 0;
 
308
 
 
309
      if (xzret == XZ_STREAM_END)       /* Stream end, EOF.  */
 
310
        break;
 
311
    }
 
312
 
 
313
  if (ret >= 0)
 
314
    xzio->saved_offset = file->offset + ret;
 
315
 
 
316
  return ret;
 
317
}
 
318
 
 
319
/* Release everything, including the underlying file object.  */
 
320
static grub_err_t
 
321
grub_xzio_close (grub_file_t file)
 
322
{
 
323
  grub_xzio_t xzio = file->data;
 
324
 
 
325
  xz_dec_end (xzio->dec);
 
326
 
 
327
  grub_file_close (xzio->file);
 
328
  grub_free (xzio);
 
329
 
 
330
  /* Device must not be closed twice.  */
 
331
  file->device = 0;
 
332
  return grub_errno;
 
333
}
 
334
 
 
335
static struct grub_fs grub_xzio_fs = {
 
336
  .name = "xzio",
 
337
  .dir = 0,
 
338
  .open = 0,
 
339
  .read = grub_xzio_read,
 
340
  .close = grub_xzio_close,
 
341
  .label = 0,
 
342
  .next = 0
 
343
};
 
344
 
 
345
GRUB_MOD_INIT (xzio)
 
346
{
 
347
  grub_file_filter_register (GRUB_FILE_FILTER_XZIO, grub_xzio_open);
 
348
}
 
349
 
 
350
GRUB_MOD_FINI (xzio)
 
351
{
 
352
  grub_file_filter_unregister (GRUB_FILE_FILTER_XZIO);
 
353
}