]> wimlib.net Git - wimlib/blob - src/header.c
2524793d5c66b89de94d45a298cde5c4647a0787
[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 #include "wimlib_internal.h"
27 #include "buffer_io.h"
28 #include <limits.h>
29
30 /* First 8 bytes in every WIM file. */
31 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = {
32                         'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
33
34 /* Reads the header from a WIM file.  */
35 int
36 read_header(filedes_t in_fd, struct wim_header *hdr, int open_flags)
37 {
38         size_t bytes_read;
39         u8 buf[WIM_HEADER_DISK_SIZE];
40         const void *p;
41
42         u32 hdr_size;
43         u32 wim_version;
44         u32 chunk_size;
45
46         DEBUG("Reading WIM header.");
47
48         bytes_read = full_pread(in_fd, buf, WIM_HEADER_DISK_SIZE, 0);
49
50         if (bytes_read != WIM_HEADER_DISK_SIZE) {
51                 ERROR_WITH_ERRNO("Error reading WIM header");
52                 return WIMLIB_ERR_READ;
53         }
54
55         p = buf;
56         if (memcmp(p, wim_magic_chars, WIM_MAGIC_LEN)) {
57                 ERROR("Invalid magic characters in WIM header");
58                 return WIMLIB_ERR_NOT_A_WIM_FILE;
59         }
60
61         /* Byte 8 */
62         p = get_u32(p + 8, &hdr_size);
63         if (hdr_size != WIM_HEADER_DISK_SIZE) {
64                 ERROR("Header is %u bytes (expected %u bytes)",
65                       hdr_size, WIM_HEADER_DISK_SIZE);
66                 return WIMLIB_ERR_INVALID_HEADER_SIZE;
67         }
68
69         /* Byte 12 */
70         p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
71         if (wim_version != WIM_VERSION) {
72                 ERROR("The WIM header says the WIM version is %u, but wimlib "
73                       "only knows about version %u",
74                       wim_version, WIM_VERSION);
75                 return WIMLIB_ERR_UNKNOWN_VERSION;
76         }
77
78         p = get_u32(p, &hdr->flags);
79         p = get_u32(p, &chunk_size);
80         if (chunk_size != WIM_CHUNK_SIZE &&
81             (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
82                 ERROR("Unexpected chunk size of %u! Ask the author to "
83                       "implement support for other chunk sizes.",
84                       chunk_size);
85                 ERROR("(Or it might just be that the WIM header is invalid.)");
86                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
87         }
88
89         p = get_bytes(p, WIM_GID_LEN, hdr->guid);
90         p = get_u16(p, &hdr->part_number);
91         p = get_u16(p, &hdr->total_parts);
92
93         if (hdr->total_parts == 0 ||
94             hdr->part_number == 0 ||
95             hdr->part_number > hdr->total_parts)
96         {
97                 ERROR("Invalid WIM part number: %hu of %hu",
98                       hdr->part_number, hdr->total_parts);
99                 return WIMLIB_ERR_INVALID_PART_NUMBER;
100         }
101
102         if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
103             hdr->total_parts != 1)
104         {
105                 ERROR("This WIM is part %u of a %u-part WIM",
106                       hdr->part_number, hdr->total_parts);
107                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
108         }
109
110         p = get_u32(p, &hdr->image_count);
111
112         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
113               hdr->part_number, hdr->total_parts, hdr->image_count);
114
115         if (hdr->image_count >= INT_MAX) {
116                 ERROR("Invalid image count (%u)", hdr->image_count);
117                 return WIMLIB_ERR_IMAGE_COUNT;
118         }
119
120         /* Byte 48 */
121
122         p = get_resource_entry(p, &hdr->lookup_table_res_entry);
123         p = get_resource_entry(p, &hdr->xml_res_entry);
124         p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
125
126         /* Byte 120 */
127
128         p = get_u32(p, &hdr->boot_idx);
129
130         /* Byte 124 */
131
132         p = get_resource_entry(p, &hdr->integrity);
133
134         /* Byte 148 */
135
136         /* 60 bytes of unused stuff. */
137
138         /* Byte 208 */
139
140         return 0;
141 }
142
143 /*
144  * Writes the header for a WIM file.
145  *
146  * @hdr:        A pointer to a struct wim_header structure that describes the header.
147  * @out_fd:     The file descriptor to the WIM file, opened for writing.
148  * 
149  * Returns zero on success, nonzero on failure.
150  */
151 int
152 write_header(const struct wim_header *hdr, int out_fd)
153 {
154         u8 buf[WIM_HEADER_DISK_SIZE];
155         u8 *p;
156         DEBUG("Writing WIM header.");
157
158         p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
159         p = put_u32(p, WIM_HEADER_DISK_SIZE);
160         p = put_u32(p, WIM_VERSION);
161         p = put_u32(p, hdr->flags);
162         p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
163                                 WIM_CHUNK_SIZE : 0);
164         /* Byte 24 */
165
166         p = put_bytes(p, WIM_GID_LEN, hdr->guid);
167         p = put_u16(p, hdr->part_number);
168
169         /* Byte 40 */
170
171         p = put_u16(p, hdr->total_parts);
172         p = put_u32(p, hdr->image_count);
173         p = put_resource_entry(p, &hdr->lookup_table_res_entry);
174         p = put_resource_entry(p, &hdr->xml_res_entry);
175         p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
176         p = put_u32(p, hdr->boot_idx);
177         p = put_resource_entry(p, &hdr->integrity);
178         p = put_zeroes(p, WIM_UNUSED_LEN);
179         wimlib_assert(p - buf == sizeof(buf));
180
181         if (full_pwrite(out_fd, buf, sizeof(buf), 0) != sizeof(buf)) {
182                 ERROR_WITH_ERRNO("Failed to write WIM header");
183                 return WIMLIB_ERR_WRITE;
184         }
185         DEBUG("Done writing WIM header");
186         return 0;
187 }
188
189 /*
190  * Initializes the header for a WIM file.
191  */
192 int
193 init_header(struct wim_header *hdr, int ctype)
194 {
195         memset(hdr, 0, sizeof(struct wim_header));
196         switch (ctype) {
197         case WIMLIB_COMPRESSION_TYPE_NONE:
198                 hdr->flags = 0;
199                 break;
200         case WIMLIB_COMPRESSION_TYPE_LZX:
201                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
202                              WIM_HDR_FLAG_COMPRESS_LZX;
203                 break;
204         case WIMLIB_COMPRESSION_TYPE_XPRESS:
205                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
206                              WIM_HDR_FLAG_COMPRESS_XPRESS;
207                 break;
208         default:
209                 ERROR("Invalid compression type specified (%d)", ctype);
210                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
211         }
212         hdr->total_parts = 1;
213         hdr->part_number = 1;
214         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
215         return 0;
216 }
217
218 struct hdr_flag {
219         u32 flag;
220         const char *name;
221 };
222 struct hdr_flag hdr_flags[] = {
223         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
224         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
225         {WIM_HDR_FLAG_READONLY,         "READONLY"},
226         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
227         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
228         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
229         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
230         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
231         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
232         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
233         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
234 };
235
236 /* Prints information from the header of the WIM file associated with @w. */
237 WIMLIBAPI void
238 wimlib_print_header(const WIMStruct *w)
239 {
240         const struct wim_header *hdr = &w->hdr;
241
242         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
243         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
244         tprintf(T("Version                     = 0x%x\n"), WIM_VERSION);
245
246         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
247         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
248                 if (hdr_flags[i].flag & hdr->flags)
249                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
250
251         tprintf(T("Chunk Size                  = %u\n"), WIM_CHUNK_SIZE);
252         tfputs (T("GUID                        = "), stdout);
253         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
254         tputchar(T('\n'));
255         tprintf(T("Part Number                 = %hu\n"), w->hdr.part_number);
256         tprintf(T("Total Parts                 = %hu\n"), w->hdr.total_parts);
257         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
258         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
259                                 (u64)hdr->lookup_table_res_entry.size);
260         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
261                                 (u8)hdr->lookup_table_res_entry.flags);
262         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
263                                 hdr->lookup_table_res_entry.offset);
264         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
265                                 hdr->lookup_table_res_entry.original_size);
266         tprintf(T("XML Data Size               = %"PRIu64"\n"),
267                                 (u64)hdr->xml_res_entry.size);
268         tprintf(T("XML Data Flags              = 0x%hhx\n"),
269                                 (u8)hdr->xml_res_entry.flags);
270         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
271                                 hdr->xml_res_entry.offset);
272         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
273                                 hdr->xml_res_entry.original_size);
274         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
275                                 (u64)hdr->boot_metadata_res_entry.size);
276         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
277                                 (u8)hdr->boot_metadata_res_entry.flags);
278         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
279                                 hdr->boot_metadata_res_entry.offset);
280         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
281                                 hdr->boot_metadata_res_entry.original_size);
282         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
283         tprintf(T("Integrity Size              = %"PRIu64"\n"),
284                                 (u64)hdr->integrity.size);
285         tprintf(T("Integrity Flags             = 0x%hhx\n"),
286                                 (u8)hdr->integrity.flags);
287         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
288                                 hdr->integrity.offset);
289         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
290                                 hdr->integrity.original_size);
291 }