]> wimlib.net Git - wimlib/blob - src/header.c
Support for splitting WIMs
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  *
6  * Copyright (C) 2010 Carl Thijssen
7  * Copyright (C) 2012 Eric Biggers
8  *
9  * wimlib - Library for working with WIM files 
10  *
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 2.1 of the License, or (at your option) any
14  * later version.
15  *
16  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License along
21  * with this library; if not, write to the Free Software Foundation, Inc., 59
22  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
23  */
24
25 #include "wimlib_internal.h"
26 #include "io.h"
27
28 /* First 8 bytes in every WIM file. */
29 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = { 
30                         'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
31
32 /* Reads the header for a WIM file.  */
33 int read_header(FILE *fp, struct wim_header *hdr, int split_ok)
34 {
35         size_t bytes_read;
36         u8 buf[WIM_HEADER_DISK_SIZE];
37         size_t hdr_rem_size;
38         const u8 *p;
39
40         u32 hdr_size;
41         u32 wim_version;
42         u32 chunk_size;
43         u16 part_number;
44         u16 total_parts;
45
46         DEBUG("Reading WIM header.\n");
47         
48         bytes_read = fread(buf, 1, WIM_MAGIC_LEN, fp);
49
50         if (bytes_read != WIM_MAGIC_LEN)
51                 goto err;
52
53         /* Byte 8 */
54
55         if (memcmp(buf, wim_magic_chars, WIM_MAGIC_LEN) != 0) {
56                 ERROR("Invalid magic characters in WIM header\n");
57                 return WIMLIB_ERR_NOT_A_WIM_FILE;
58         }
59
60         bytes_read = fread(&hdr_size, 1, sizeof(u32), fp);
61         if (bytes_read != sizeof(u32))
62                 goto err;
63
64         TO_LE32(hdr_size);
65
66         /* Byte 12 */
67
68         if (hdr_size != WIM_HEADER_DISK_SIZE) {
69                 DEBUG("ERROR: Header is size %u (expected %u)\n",
70                                 hdr_size, WIM_HEADER_DISK_SIZE);
71                 return WIMLIB_ERR_INVALID_HEADER_SIZE;
72         }
73
74         /* Read the rest of the header into a buffer. */
75
76         hdr_rem_size = WIM_HEADER_DISK_SIZE - WIM_MAGIC_LEN - sizeof(u32);
77
78         bytes_read = fread(buf + WIM_MAGIC_LEN + sizeof(u32), 1, 
79                            hdr_rem_size, fp);
80         if (bytes_read != hdr_rem_size)
81                 goto err;
82
83         p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
84
85         if (wim_version != WIM_VERSION) {
86                 ERROR("The WIM header says the WIM version is %u, but Wimlib "
87                         "only knows about version %u.\n", wim_version, 
88                                                                 WIM_VERSION);
89                 return WIMLIB_ERR_UNKNOWN_VERSION;
90         }
91
92         p = get_u32(p, &hdr->flags);
93         p = get_u32(p, &chunk_size);
94         if (chunk_size != WIM_CHUNK_SIZE && 
95                         (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
96                 ERROR("Unexpected chunk size of %u! Ask the author to "
97                                 "implement support for other chunk sizes. "
98                                 "(Or it might just be that the WIM header is "
99                                 "invalid.)\n", chunk_size);
100                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
101         }
102
103         p = get_bytes(p, WIM_GID_LEN, hdr->guid);
104         p = get_u16(p, &hdr->part_number);
105         p = get_u16(p, &hdr->total_parts);
106
107         if (!split_ok && (hdr->part_number != 1 || hdr->total_parts != 1)) {
108                 ERROR("This WIM is part %u of a %u-part WIM.\n",
109                         hdr->part_number, hdr->total_parts);
110                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
111         }
112
113         p = get_u32(p, &hdr->image_count);
114
115         DEBUG("part_number = %u, total_parts = %u, image_count = %u\n",
116                         hdr->part_number, hdr->total_parts, hdr->image_count);
117
118         /* Byte 48 */
119
120         p = get_resource_entry(p, &hdr->lookup_table_res_entry);
121         p = get_resource_entry(p, &hdr->xml_res_entry);
122         p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
123
124         /* Byte 120 */
125
126         p = get_u32(p, &hdr->boot_idx);
127
128         /* Byte 124 */
129
130         p = get_resource_entry(p, &hdr->integrity);
131
132         /* Byte 148 */
133
134         /* 60 bytes of unused stuff. */
135
136         /* Byte 208 */
137
138         return 0;
139
140 err:
141         if (feof(fp))
142                 ERROR("Unexpected EOF while reading WIM header!\n");
143         else
144                 ERROR("Error reading WIM header: %m\n");
145         return WIMLIB_ERR_READ;
146 }
147
148 /* 
149  * Writes the header for a WIM file.
150  *
151  * @hdr:        A pointer to a struct wim_header structure that describes the header.
152  * @out:        The FILE* for the output file, positioned at the appropriate
153  *              place (the beginning of the file).
154  * @return:     True on success, false on failure.
155  */
156 int write_header(const struct wim_header *hdr, FILE *out)
157 {
158         u8 buf[WIM_HEADER_DISK_SIZE];
159         u8 *p;
160         DEBUG("Writing WIM header.\n");
161
162         p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
163         p = put_u32(p, WIM_HEADER_DISK_SIZE);
164         p = put_u32(p, WIM_VERSION);
165         p = put_u32(p, hdr->flags);
166         p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ? 
167                                 WIM_CHUNK_SIZE : 0);
168         /* byte 24 */
169
170         p = put_bytes(p, WIM_GID_LEN, hdr->guid);
171         p = put_u16(p, hdr->part_number);
172
173         /* byte 40 */
174
175         p = put_u16(p, hdr->total_parts);
176         p = put_u32(p, hdr->image_count);
177         p = put_resource_entry(p, &hdr->lookup_table_res_entry);
178         p = put_resource_entry(p, &hdr->xml_res_entry);
179         p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
180         p = put_u32(p, hdr->boot_idx);
181         p = put_resource_entry(p, &hdr->integrity);
182         memset(p, 0, WIM_UNUSED_LEN);
183         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
184                 DEBUG("Failed to write WIM header: %m\n");
185                 return WIMLIB_ERR_WRITE;
186         }
187         return 0;
188 }
189
190 /*
191  * Initializes the header for a WIM file.
192  */
193 int init_header(struct wim_header *hdr, int ctype)
194 {
195         memset(hdr, 0, sizeof(struct wim_header));
196         switch (ctype) {
197         case WIM_COMPRESSION_TYPE_NONE:
198                 hdr->flags = 0;
199                 break;
200         case WIM_COMPRESSION_TYPE_LZX:
201                 hdr->flags = WIM_HDR_FLAG_COMPRESSION | 
202                         WIM_HDR_FLAG_COMPRESS_LZX;
203                 break;
204         case WIM_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)!\n", 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 wimlib_print_header(const WIMStruct *w)
238 {
239         const struct wim_header *hdr = &w->hdr;
240         uint i;
241
242         printf("Magic Characters            = MSWIM\\000\\000\\000\n");
243         printf("Header Size                 = %u\n", WIM_HEADER_DISK_SIZE);
244         printf("Version                     = 0x%x\n", WIM_VERSION);
245
246         printf("Flags                       = 0x%x\n", hdr->flags);
247         for (i = 0; i < ARRAY_LEN(hdr_flags); i++)
248                 if (hdr_flags[i].flag & hdr->flags)
249                         printf("    WIM_HDR_FLAG_%s is set\n", hdr_flags[i].name);
250
251         printf("Chunk Size                  = %u\n", WIM_CHUNK_SIZE);
252         fputs ("GUID                        = ", stdout);
253         print_byte_field(hdr->guid, WIM_GID_LEN);
254         putchar('\n');
255         printf("Part Number                 = %hu\n", 1);
256         printf("Total Parts                 = %hu\n", 1);
257         printf("Image Count                 = %u\n", hdr->image_count);
258         printf("Lookup Table Size           = %"PRIu64"\n", 
259                                 (u64)hdr->lookup_table_res_entry.size);
260         printf("Lookup Table Flags          = 0x%hhx\n", 
261                                 hdr->lookup_table_res_entry.flags);
262         printf("Lookup Table Offset         = %"PRIu64"\n",
263                                 hdr->lookup_table_res_entry.offset);
264         printf("Lookup Table Original_size  = %"PRIu64"\n", 
265                                 hdr->lookup_table_res_entry.original_size);
266         printf("XML Data Size               = %"PRIu64"\n", 
267                                 (u64)hdr->xml_res_entry.size);
268         printf("XML Data Flags              = 0x%hhx\n", 
269                                 hdr->xml_res_entry.flags);
270         printf("XML Data Offset             = %"PRIu64"\n", 
271                                 hdr->xml_res_entry.offset);
272         printf("XML Data Original Size      = %"PRIu64"\n", 
273                                 hdr->xml_res_entry.original_size);
274         printf("Boot Metadata Size          = %"PRIu64"\n", 
275                                 (u64)hdr->boot_metadata_res_entry.size);
276         printf("Boot Metadata Flags         = 0x%hhx\n", 
277                                 hdr->boot_metadata_res_entry.flags);
278         printf("Boot Metadata Offset        = %"PRIu64"\n", 
279                                 hdr->boot_metadata_res_entry.offset);
280         printf("Boot Metadata Original Size = %"PRIu64"\n", 
281                                 hdr->boot_metadata_res_entry.original_size);
282         printf("Boot Index                  = %u\n", hdr->boot_idx);
283         printf("Integrity Size              = %"PRIu64"\n", 
284                                         (u64)hdr->integrity.size);
285         printf("Integrity Flags             = 0x%hhx\n", 
286                                         hdr->integrity.flags);
287         printf("Integrity Offset            = %"PRIu64"\n", 
288                                         hdr->integrity.offset);
289         printf("Integrity Original_size     = %"PRIu64"\n", 
290                                         hdr->integrity.original_size);
291 }