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