]> wimlib.net Git - wimlib/blob - src/header.c
WIMBoot: Update WimOverlay.dat directly when WOF not running
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib.h"
31 #include "wimlib/assert.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/error.h"
34 #include "wimlib/file_io.h"
35 #include "wimlib/header.h"
36 #include "wimlib/util.h"
37 #include "wimlib/wim.h"
38
39 #include <limits.h>
40 #include <string.h>
41 #include <unistd.h>
42 #ifdef HAVE_ALLOCA_H
43 #  include <alloca.h>
44 #else
45 #  include <stdlib.h>
46 #endif
47
48 /*
49  * Reads the header from a WIM file.
50  *
51  * @wim
52  *      WIM to read the header from; @wim->in_fd must be positioned at the
53  *      offset at which to read the header.
54  *
55  * @hdr
56  *      Structure to read the header into.
57  *
58  * Return values:
59  *      WIMLIB_ERR_SUCCESS (0)
60  *      WIMLIB_ERR_IMAGE_COUNT
61  *      WIMLIB_ERR_INVALID_PART_NUMBER
62  *      WIMLIB_ERR_NOT_A_WIM_FILE
63  *      WIMLIB_ERR_READ
64  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE
65  *      WIMLIB_ERR_UNKNOWN_VERSION
66  */
67 int
68 read_wim_header(WIMStruct *wim, struct wim_header *hdr)
69 {
70         struct wim_header_disk disk_hdr _aligned_attribute(8);
71         struct filedes *in_fd = &wim->in_fd;
72         const tchar *filename = wim->filename;
73         int ret;
74         tchar *pipe_str;
75
76         wimlib_assert(in_fd->offset == 0);
77
78         if (filename == NULL) {
79                 pipe_str = alloca(40);
80                 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
81                 filename = pipe_str;
82         }
83
84         BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
85
86         DEBUG("Reading WIM header from \"%"TS"\"", filename);
87
88         ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
89         if (ret)
90                 goto read_error;
91
92         hdr->magic = le64_to_cpu(disk_hdr.magic);
93
94         if (hdr->magic != WIM_MAGIC) {
95                 if (hdr->magic == PWM_MAGIC) {
96                         /* Pipable WIM:  Use header at end instead, unless
97                          * actually reading from a pipe.  */
98                         if (!in_fd->is_pipe) {
99                                 lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END);
100                                 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
101                                 if (ret)
102                                         goto read_error;
103                         }
104                 } else {
105                         ERROR("\"%"TS"\": Invalid magic characters in header", filename);
106                         return WIMLIB_ERR_NOT_A_WIM_FILE;
107                 }
108         }
109
110         if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
111                 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
112                       filename, le32_to_cpu(disk_hdr.hdr_size));
113                 return WIMLIB_ERR_INVALID_HEADER;
114         }
115
116         hdr->wim_version = le32_to_cpu(disk_hdr.wim_version);
117         if (hdr->wim_version != WIM_VERSION_DEFAULT &&
118             hdr->wim_version != WIM_VERSION_PACKED_STREAMS)
119         {
120                 ERROR("\"%"TS"\": Unknown WIM version: %u", hdr->wim_version);
121                 return WIMLIB_ERR_UNKNOWN_VERSION;
122         }
123
124         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
125         hdr->chunk_size = le32_to_cpu(disk_hdr.chunk_size);
126         memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
127         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
128         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
129
130         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
131             hdr->part_number > hdr->total_parts)
132         {
133                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
134                       filename, hdr->part_number, hdr->total_parts);
135                 return WIMLIB_ERR_INVALID_PART_NUMBER;
136         }
137
138         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
139
140         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
141               hdr->part_number, hdr->total_parts, hdr->image_count);
142
143         if (hdr->image_count >= INT_MAX) {
144                 ERROR("\"%"TS"\": Invalid image count (%u)",
145                       filename, hdr->image_count);
146                 return WIMLIB_ERR_IMAGE_COUNT;
147         }
148
149         get_wim_reshdr(&disk_hdr.lookup_table_reshdr, &hdr->lookup_table_reshdr);
150         get_wim_reshdr(&disk_hdr.xml_data_reshdr, &hdr->xml_data_reshdr);
151         get_wim_reshdr(&disk_hdr.boot_metadata_reshdr, &hdr->boot_metadata_reshdr);
152         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
153         get_wim_reshdr(&disk_hdr.integrity_table_reshdr, &hdr->integrity_table_reshdr);
154         return 0;
155
156 read_error:
157         ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
158         return ret;
159 }
160
161 /* Writes the header for a WIM file at the specified offset.  If the offset
162  * specified is the current one, the position is advanced by the size of the
163  * header.  */
164 int
165 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
166                            off_t offset)
167 {
168         struct wim_header_disk disk_hdr _aligned_attribute(8);
169         int ret;
170
171         DEBUG("Writing %sWIM header at offset %"PRIu64,
172               ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
173               offset);
174
175         disk_hdr.magic = cpu_to_le64(hdr->magic);
176         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
177         disk_hdr.wim_version = cpu_to_le32(hdr->wim_version);
178         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
179         if (hdr->flags & WIM_HDR_FLAG_COMPRESSION)
180                 disk_hdr.chunk_size = cpu_to_le32(hdr->chunk_size);
181         else
182                 disk_hdr.chunk_size = 0;
183         memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
184
185         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
186         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
187         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
188         put_wim_reshdr(&hdr->lookup_table_reshdr, &disk_hdr.lookup_table_reshdr);
189         put_wim_reshdr(&hdr->xml_data_reshdr, &disk_hdr.xml_data_reshdr);
190         put_wim_reshdr(&hdr->boot_metadata_reshdr, &disk_hdr.boot_metadata_reshdr);
191         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
192         put_wim_reshdr(&hdr->integrity_table_reshdr, &disk_hdr.integrity_table_reshdr);
193         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
194
195         if (offset == out_fd->offset)
196                 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
197         else
198                 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
199         if (ret)
200                 ERROR_WITH_ERRNO("Failed to write WIM header");
201         return ret;
202 }
203
204 /* Writes the header for a WIM file at the output file descriptor's current
205  * offset.  */
206 int
207 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
208 {
209         return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
210 }
211
212 /* Update just the wim_flags field. */
213 int
214 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
215 {
216         le32 flags = cpu_to_le32(hdr_flags);
217
218         return full_pwrite(out_fd, &flags, sizeof(flags),
219                            offsetof(struct wim_header_disk, wim_flags));
220 }
221
222 int
223 set_wim_hdr_cflags(int ctype, struct wim_header *hdr)
224 {
225         hdr->flags &= ~(WIM_HDR_FLAG_COMPRESSION |
226                         WIM_HDR_FLAG_COMPRESS_LZX |
227                         WIM_HDR_FLAG_COMPRESS_RESERVED |
228                         WIM_HDR_FLAG_COMPRESS_XPRESS |
229                         WIM_HDR_FLAG_COMPRESS_LZMS |
230                         WIM_HDR_FLAG_COMPRESS_XPRESS_2);
231         switch (ctype) {
232
233         case WIMLIB_COMPRESSION_TYPE_NONE:
234                 return 0;
235
236         case WIMLIB_COMPRESSION_TYPE_LZX:
237                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZX;
238                 return 0;
239
240         case WIMLIB_COMPRESSION_TYPE_XPRESS:
241                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_XPRESS;
242                 return 0;
243
244         case WIMLIB_COMPRESSION_TYPE_LZMS:
245                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZMS;
246                 return 0;
247
248         default:
249                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
250         }
251 }
252
253 /*
254  * Initializes the header for a WIM file.
255  */
256 int
257 init_wim_header(struct wim_header *hdr, int ctype, u32 chunk_size)
258 {
259         memset(hdr, 0, sizeof(struct wim_header));
260         hdr->magic = WIM_MAGIC;
261
262         if (ctype == WIMLIB_COMPRESSION_TYPE_LZMS)
263                 hdr->wim_version = WIM_VERSION_PACKED_STREAMS;
264         else
265                 hdr->wim_version = WIM_VERSION_DEFAULT;
266         if (set_wim_hdr_cflags(ctype, hdr)) {
267                 ERROR("Invalid compression type specified (%d)", ctype);
268                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
269         }
270         hdr->chunk_size = chunk_size;
271         hdr->total_parts = 1;
272         hdr->part_number = 1;
273         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
274         return 0;
275 }
276
277 struct hdr_flag {
278         u32 flag;
279         const char *name;
280 };
281 struct hdr_flag hdr_flags[] = {
282         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
283         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
284         {WIM_HDR_FLAG_READONLY,         "READONLY"},
285         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
286         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
287         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
288         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
289         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
290         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
291         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
292         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
293         {WIM_HDR_FLAG_COMPRESS_LZMS,    "COMPRESS_LZMS"},
294         {WIM_HDR_FLAG_COMPRESS_XPRESS_2,"COMPRESS_XPRESS_2"},
295 };
296
297 /* API function documented in wimlib.h  */
298 WIMLIBAPI void
299 wimlib_print_header(const WIMStruct *wim)
300 {
301         const struct wim_header *hdr = &wim->hdr;
302
303         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
304         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
305         tprintf(T("Version                     = 0x%x\n"), hdr->wim_version);
306
307         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
308         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
309                 if (hdr_flags[i].flag & hdr->flags)
310                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
311
312         tprintf(T("Chunk Size                  = %u\n"), hdr->chunk_size);
313         tfputs (T("GUID                        = "), stdout);
314         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
315         tputchar(T('\n'));
316         tprintf(T("Part Number                 = %hu\n"), hdr->part_number);
317         tprintf(T("Total Parts                 = %hu\n"), hdr->total_parts);
318         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
319         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
320                                 (u64)hdr->lookup_table_reshdr.size_in_wim);
321         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
322                                 (u8)hdr->lookup_table_reshdr.flags);
323         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
324                                 hdr->lookup_table_reshdr.offset_in_wim);
325         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
326                                 hdr->lookup_table_reshdr.uncompressed_size);
327         tprintf(T("XML Data Size               = %"PRIu64"\n"),
328                                 (u64)hdr->xml_data_reshdr.size_in_wim);
329         tprintf(T("XML Data Flags              = 0x%hhx\n"),
330                                 (u8)hdr->xml_data_reshdr.flags);
331         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
332                                 hdr->xml_data_reshdr.offset_in_wim);
333         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
334                                 hdr->xml_data_reshdr.uncompressed_size);
335         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
336                                 (u64)hdr->boot_metadata_reshdr.size_in_wim);
337         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
338                                 (u8)hdr->boot_metadata_reshdr.flags);
339         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
340                                 hdr->boot_metadata_reshdr.offset_in_wim);
341         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
342                                 hdr->boot_metadata_reshdr.uncompressed_size);
343         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
344         tprintf(T("Integrity Size              = %"PRIu64"\n"),
345                                 (u64)hdr->integrity_table_reshdr.size_in_wim);
346         tprintf(T("Integrity Flags             = 0x%hhx\n"),
347                                 (u8)hdr->integrity_table_reshdr.flags);
348         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
349                                 hdr->integrity_table_reshdr.offset_in_wim);
350         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
351                                 hdr->integrity_table_reshdr.uncompressed_size);
352 }