4924b505dfba8f6e455733ddcb0e67722ba70e9f
[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  * @filename
52  *      Name of the WIM file (for error/debug messages only), or NULL if reading
53  *      directly from file descriptor.
54  * @in_fd
55  *      File descriptor, positioned at offset 0, to read the header from.
56  * @hdr
57  *      Structure to read the header into.
58  *
59  * Return values:
60  *      WIMLIB_ERR_SUCCESS (0)
61  *      WIMLIB_ERR_IMAGE_COUNT
62  *      WIMLIB_ERR_INVALID_CHUNK_SIZE
63  *      WIMLIB_ERR_INVALID_PART_NUMBER
64  *      WIMLIB_ERR_NOT_A_WIM_FILE
65  *      WIMLIB_ERR_READ
66  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE
67  *      WIMLIB_ERR_UNKNOWN_VERSION
68  */
69 int
70 read_wim_header(const tchar *filename, struct filedes *in_fd,
71                 struct wim_header *hdr)
72 {
73         struct wim_header_disk disk_hdr _aligned_attribute(8);
74         int ret;
75         tchar *pipe_str;
76
77         wimlib_assert(in_fd->offset == 0);
78
79         if (filename == NULL) {
80                 pipe_str = alloca(40);
81                 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
82                 filename = pipe_str;
83         }
84
85         BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
86
87         DEBUG("Reading WIM header from \"%"TS"\"", filename);
88
89         ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
90         if (ret)
91                 goto read_error;
92
93         if (disk_hdr.magic != WIM_MAGIC) {
94                 if (disk_hdr.magic == PWM_MAGIC) {
95                         /* Pipable WIM:  Use header at end instead, unless
96                          * actually reading from a pipe.  */
97                         if (!in_fd->is_pipe) {
98                                 lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END);
99                                 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
100                                 if (ret)
101                                         goto read_error;
102                         }
103                 } else {
104                         ERROR("\"%"TS"\": Invalid magic characters in header", filename);
105                         return WIMLIB_ERR_NOT_A_WIM_FILE;
106                 }
107         }
108         hdr->magic = disk_hdr.magic;
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         if (le32_to_cpu(disk_hdr.wim_version) != WIM_VERSION) {
117                 ERROR("\"%"TS"\": The WIM header says the WIM version is %u, "
118                       "but wimlib only knows about version %u",
119                       filename, le32_to_cpu(disk_hdr.wim_version), WIM_VERSION);
120                 if (le32_to_cpu(disk_hdr.wim_flags) & WIM_HDR_FLAG_COMPRESS_LZMS) {
121                         ERROR("\"%"TS"\": This WIM uses LZMS compression, "
122                               "which is not supported by wimlib.", filename);
123                 }
124                 return WIMLIB_ERR_UNKNOWN_VERSION;
125         }
126
127         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
128         hdr->chunk_size = le32_to_cpu(disk_hdr.chunk_size);
129         memcpy(hdr->guid, disk_hdr.guid, WIM_GID_LEN);
130         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
131         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
132
133         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
134             hdr->part_number > hdr->total_parts)
135         {
136                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
137                       filename, hdr->part_number, hdr->total_parts);
138                 return WIMLIB_ERR_INVALID_PART_NUMBER;
139         }
140
141         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
142
143         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
144               hdr->part_number, hdr->total_parts, hdr->image_count);
145
146         if (hdr->image_count >= INT_MAX) {
147                 ERROR("\"%"TS"\": Invalid image count (%u)",
148                       filename, hdr->image_count);
149                 return WIMLIB_ERR_IMAGE_COUNT;
150         }
151
152         get_resource_entry(&disk_hdr.lookup_table_res_entry, &hdr->lookup_table_res_entry);
153         get_resource_entry(&disk_hdr.xml_data_res_entry, &hdr->xml_res_entry);
154         get_resource_entry(&disk_hdr.boot_metadata_res_entry, &hdr->boot_metadata_res_entry);
155         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
156         get_resource_entry(&disk_hdr.integrity_table_res_entry, &hdr->integrity);
157         return 0;
158
159 read_error:
160         ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
161         return ret;
162 }
163
164 /* Writes the header for a WIM file at the specified offset.  If the offset
165  * specified is the current one, the position is advanced by the size of the
166  * header.  */
167 int
168 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
169                            off_t offset)
170 {
171         struct wim_header_disk disk_hdr _aligned_attribute(8);
172         int ret;
173
174         DEBUG("Writing %sWIM header at offset %"PRIu64,
175               ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
176               offset);
177
178         disk_hdr.magic = hdr->magic;
179         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
180         disk_hdr.wim_version = cpu_to_le32(WIM_VERSION);
181         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
182         if (hdr->flags & WIM_HDR_FLAG_COMPRESSION)
183                 disk_hdr.chunk_size = cpu_to_le32(hdr->chunk_size);
184         else
185                 disk_hdr.chunk_size = 0;
186         memcpy(disk_hdr.guid, hdr->guid, WIM_GID_LEN);
187
188         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
189         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
190         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
191         put_resource_entry(&hdr->lookup_table_res_entry, &disk_hdr.lookup_table_res_entry);
192         put_resource_entry(&hdr->xml_res_entry, &disk_hdr.xml_data_res_entry);
193         put_resource_entry(&hdr->boot_metadata_res_entry, &disk_hdr.boot_metadata_res_entry);
194         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
195         put_resource_entry(&hdr->integrity, &disk_hdr.integrity_table_res_entry);
196         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
197
198         if (offset == out_fd->offset)
199                 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
200         else
201                 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
202         if (ret)
203                 ERROR_WITH_ERRNO("Failed to write WIM header");
204         return ret;
205 }
206
207 /* Writes the header for a WIM file at the output file descriptor's current
208  * offset.  */
209 int
210 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
211 {
212         return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
213 }
214
215 /* Update just the wim_flags field. */
216 int
217 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
218 {
219         le32 flags = cpu_to_le32(hdr_flags);
220
221         return full_pwrite(out_fd, &flags, sizeof(flags),
222                            offsetof(struct wim_header_disk, wim_flags));
223 }
224
225 int
226 set_wim_hdr_cflags(int ctype, struct wim_header *hdr)
227 {
228         hdr->flags &= ~(WIM_HDR_FLAG_COMPRESSION |
229                         WIM_HDR_FLAG_COMPRESS_LZX |
230                         WIM_HDR_FLAG_COMPRESS_RESERVED |
231                         WIM_HDR_FLAG_COMPRESS_XPRESS |
232                         WIM_HDR_FLAG_COMPRESS_LZMS);
233         switch (ctype) {
234
235         case WIMLIB_COMPRESSION_TYPE_NONE:
236                 return 0;
237
238         case WIMLIB_COMPRESSION_TYPE_LZX:
239                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZX;
240                 return 0;
241
242         case WIMLIB_COMPRESSION_TYPE_XPRESS:
243                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_XPRESS;
244                 return 0;
245
246         default:
247                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
248         }
249 }
250
251 /*
252  * Initializes the header for a WIM file.
253  */
254 int
255 init_wim_header(struct wim_header *hdr, int ctype, u32 chunk_size)
256 {
257         memset(hdr, 0, sizeof(struct wim_header));
258         hdr->magic = WIM_MAGIC;
259         if (set_wim_hdr_cflags(ctype, hdr)) {
260                 ERROR("Invalid compression type specified (%d)", ctype);
261                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
262         }
263         hdr->chunk_size = chunk_size;
264         hdr->total_parts = 1;
265         hdr->part_number = 1;
266         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
267         return 0;
268 }
269
270 struct hdr_flag {
271         u32 flag;
272         const char *name;
273 };
274 struct hdr_flag hdr_flags[] = {
275         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
276         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
277         {WIM_HDR_FLAG_READONLY,         "READONLY"},
278         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
279         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
280         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
281         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
282         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
283         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
284         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
285         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
286 };
287
288 /* API function documented in wimlib.h  */
289 WIMLIBAPI void
290 wimlib_print_header(const WIMStruct *wim)
291 {
292         const struct wim_header *hdr = &wim->hdr;
293
294         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
295         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
296         tprintf(T("Version                     = 0x%x\n"), WIM_VERSION);
297
298         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
299         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
300                 if (hdr_flags[i].flag & hdr->flags)
301                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
302
303         tprintf(T("Chunk Size                  = %u\n"), wim->hdr.chunk_size);
304         tfputs (T("GUID                        = "), stdout);
305         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
306         tputchar(T('\n'));
307         tprintf(T("Part Number                 = %hu\n"), wim->hdr.part_number);
308         tprintf(T("Total Parts                 = %hu\n"), wim->hdr.total_parts);
309         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
310         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
311                                 (u64)hdr->lookup_table_res_entry.size);
312         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
313                                 (u8)hdr->lookup_table_res_entry.flags);
314         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
315                                 hdr->lookup_table_res_entry.offset);
316         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
317                                 hdr->lookup_table_res_entry.original_size);
318         tprintf(T("XML Data Size               = %"PRIu64"\n"),
319                                 (u64)hdr->xml_res_entry.size);
320         tprintf(T("XML Data Flags              = 0x%hhx\n"),
321                                 (u8)hdr->xml_res_entry.flags);
322         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
323                                 hdr->xml_res_entry.offset);
324         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
325                                 hdr->xml_res_entry.original_size);
326         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
327                                 (u64)hdr->boot_metadata_res_entry.size);
328         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
329                                 (u8)hdr->boot_metadata_res_entry.flags);
330         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
331                                 hdr->boot_metadata_res_entry.offset);
332         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
333                                 hdr->boot_metadata_res_entry.original_size);
334         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
335         tprintf(T("Integrity Size              = %"PRIu64"\n"),
336                                 (u64)hdr->integrity.size);
337         tprintf(T("Integrity Flags             = 0x%hhx\n"),
338                                 (u8)hdr->integrity.flags);
339         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
340                                 hdr->integrity.offset);
341         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
342                                 hdr->integrity.original_size);
343 }