4 * Support for creating WIMBoot pointer files.
6 * See http://technet.microsoft.com/en-us/library/dn594399.aspx for general
7 * information about WIMBoot.
9 * Note that WIMBoot pointer files are actually implemented on top of the
10 * Windows Overlay File System Filter (WOF). See wof.h for more info.
14 * Copyright (C) 2014 Eric Biggers
16 * This file is part of wimlib, a library for working with WIM files.
18 * wimlib is free software; you can redistribute it and/or modify it under the
19 * terms of the GNU General Public License as published by the Free
20 * Software Foundation; either version 3 of the License, or (at your option)
23 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
24 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25 * A PARTICULAR PURPOSE. See the GNU General Public License for more
28 * You should have received a copy of the GNU General Public License
29 * along with wimlib; if not, see http://www.gnu.org/licenses/.
37 #include "wimlib/win32_common.h"
38 #include "wimlib/win32.h"
39 #include "wimlib/assert.h"
40 #include "wimlib/error.h"
41 #include "wimlib/lookup_table.h"
42 #include "wimlib/util.h"
43 #include "wimlib/wimboot.h"
44 #include "wimlib/wof.h"
47 open_file(const wchar_t *device_name, DWORD desiredAccess)
49 return CreateFile(device_name, desiredAccess,
50 FILE_SHARE_VALID_FLAGS, NULL, OPEN_EXISTING,
51 FILE_FLAG_BACKUP_SEMANTICS, NULL);
55 query_device(HANDLE h, DWORD code, void *out, DWORD out_size)
58 return DeviceIoControl(h, code, NULL, 0, out, out_size,
59 &bytes_returned, NULL);
63 * Gets partition and drive information for the specified path.
66 * Absolute path which must begin with a drive letter. For example, if the
67 * path is D:\install.wim, this function will query information about the
70 * Partition info is returned here.
72 * Drive info is returned here. The contained partition info will not be
75 * Returns 0 on success, or a positive error code on failure.
78 query_partition_and_disk_info(const wchar_t *path,
79 PARTITION_INFORMATION_EX *part_info_ret,
80 DRIVE_LAYOUT_INFORMATION_EX *drive_info_ret)
83 wchar_t vol_name[] = L"\\\\.\\X:";
84 wchar_t disk_name[] = L"\\\\?\\PhysicalDriveXXXXXXXXXX";
86 PARTITION_INFORMATION_EX part_info;
87 size_t extents_size = sizeof(VOLUME_DISK_EXTENTS) + 4 * sizeof(DISK_EXTENT);
88 VOLUME_DISK_EXTENTS *extents = alloca(extents_size);
89 size_t drive_info_size = sizeof(DRIVE_LAYOUT_INFORMATION_EX) +
90 8 * sizeof(PARTITION_INFORMATION_EX);
91 DRIVE_LAYOUT_INFORMATION_EX *drive_info = alloca(drive_info_size);
93 wimlib_assert(path[0] != L'\0' && path[1] == L':');
95 *(wcschr(vol_name, L'X')) = path[0];
97 h = open_file(vol_name, GENERIC_READ);
98 if (h == INVALID_HANDLE_VALUE) {
99 set_errno_from_GetLastError();
100 ERROR_WITH_ERRNO("\"%ls\": Can't open volume device", vol_name);
101 return WIMLIB_ERR_OPEN;
104 if (!query_device(h, IOCTL_DISK_GET_PARTITION_INFO_EX,
105 &part_info, sizeof(part_info)))
107 ERROR("\"%ls\": Can't get partition info (err=0x%08"PRIx32")",
108 vol_name, (u32)GetLastError());
110 return WIMLIB_ERR_READ;
113 if (!query_device(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
114 extents, extents_size))
116 ERROR("\"%ls\": Can't get volume extent info (err=0x%08"PRIx32")",
117 vol_name, (u32)GetLastError());
119 return WIMLIB_ERR_READ;
124 if (extents->NumberOfDiskExtents != 1) {
125 ERROR("\"%ls\": This volume has %"PRIu32" disk extents, "
126 "but this code is untested for more than 1",
127 vol_name, (u32)extents->NumberOfDiskExtents);
128 return WIMLIB_ERR_UNSUPPORTED;
131 wsprintf(wcschr(disk_name, L'X'), L"%"PRIu32,
132 extents->Extents[0].DiskNumber);
134 h = open_file(disk_name, GENERIC_READ);
135 if (h == INVALID_HANDLE_VALUE) {
136 set_errno_from_GetLastError();
137 ERROR_WITH_ERRNO("\"%ls\": Can't open disk device", disk_name);
138 return WIMLIB_ERR_OPEN;
141 if (!query_device(h, IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
142 drive_info, drive_info_size))
144 ERROR("\"%ls\": Can't get disk info (err=0x%08"PRIx32")",
145 disk_name, (u32)GetLastError());
147 return WIMLIB_ERR_READ;
152 if (drive_info->PartitionStyle != part_info.PartitionStyle) {
153 ERROR("\"%ls\", \"%ls\": Inconsistent partition table type!",
154 vol_name, disk_name);
155 return WIMLIB_ERR_UNSUPPORTED;
158 if (part_info.PartitionStyle == PARTITION_STYLE_GPT) {
159 BUILD_BUG_ON(sizeof(part_info.Gpt.PartitionId) !=
160 sizeof(drive_info->Gpt.DiskId));
161 if (!memcmp(&part_info.Gpt.PartitionId,
162 &drive_info->Gpt.DiskId,
163 sizeof(drive_info->Gpt.DiskId)))
165 ERROR("\"%ls\", \"%ls\": Partition GUID is the "
166 "same as the disk GUID???", vol_name, disk_name);
167 return WIMLIB_ERR_UNSUPPORTED;
171 if (part_info.PartitionStyle != PARTITION_STYLE_MBR &&
172 part_info.PartitionStyle != PARTITION_STYLE_GPT)
174 ERROR("\"%ls\": Unknown partition style 0x%08"PRIx32,
175 vol_name, (u32)part_info.PartitionStyle);
176 return WIMLIB_ERR_UNSUPPORTED;
179 *part_info_ret = part_info;
180 *drive_info_ret = *drive_info;
185 * Allocate a new WIM data source ID.
188 * Previous WimOverlay.dat contents, or NULL if file did not exist.
190 * Returns the new data source ID.
193 alloc_new_data_source_id(const struct WimOverlay_dat_header *old_hdr)
198 for (u64 id = 0; ; id++) {
199 for (u32 i = 0; i < old_hdr->num_entries_1; i++)
200 if (id == old_hdr->entry_1s[i].data_source_id)
209 * Calculate the size of WimOverlay.dat with one entry added.
212 * Previous WimOverlay.dat contents, or NULL if file did not exist.
214 * Size of entry_2 being added.
216 * Size will be returned here.
218 * Returns 0 on success, or WIMLIB_ERR_UNSUPPORTED if size overflows 32 bits.
221 calculate_wimoverlay_dat_size(const struct WimOverlay_dat_header *old_hdr,
222 u32 new_entry_2_size, u32 *size_ret)
227 size_64 = sizeof(struct WimOverlay_dat_header);
229 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
230 size_64 += sizeof(struct WimOverlay_dat_entry_1);
231 size_64 += old_hdr->entry_1s[i].entry_2_length;
234 size_64 += sizeof(struct WimOverlay_dat_entry_1);
235 size_64 += new_entry_2_size;
239 return WIMLIB_ERR_UNSUPPORTED;
246 * Writes @size bytes of @contents to the named file @path.
248 * Returns 0 on success; WIMLIB_ERR_OPEN or WIMLIB_ERR_WRITE on failure.
251 write_wimoverlay_dat(const wchar_t *path, const void *contents, u32 size)
256 h = CreateFile(path, GENERIC_WRITE, 0, NULL,
257 CREATE_ALWAYS, FILE_FLAG_BACKUP_SEMANTICS, NULL);
258 if (h == INVALID_HANDLE_VALUE) {
259 set_errno_from_GetLastError();
260 ERROR_WITH_ERRNO("\"%ls\": Can't open file for writing", path);
261 return WIMLIB_ERR_OPEN;
264 if (!WriteFile(h, contents, size, &bytes_written, NULL) ||
265 bytes_written != size)
267 set_errno_from_GetLastError();
268 ERROR_WITH_ERRNO("\"%ls\": Can't write file", path);
270 return WIMLIB_ERR_WRITE;
273 if (!CloseHandle(h)) {
274 set_errno_from_GetLastError();
275 ERROR_WITH_ERRNO("\"%ls\": Can't close handle", path);
276 return WIMLIB_ERR_WRITE;
283 * Generates the contents of WimOverlay.dat in memory, with one entry added.
286 * Buffer large enough to hold the new contents.
288 * Old contents of WimOverlay.dat, or NULL if it did not exist.
290 * Absolute path to the WIM file. It must begin with a drive letter; for
291 * example, D:\install.wim.
293 * GUID of the WIM, from the WIM header.
295 * Number of the image in the WIM to specify in the new entry.
296 * @new_data_source_id
297 * Data source ID to use for the new entry.
299 * Partition information for the WIM file.
301 * Disk information for the WIM file.
303 * Size, in bytes, of the new location information structure ('struct
304 * WimOverlay_dat_entry_2').
306 * Returns a pointer one past the last byte of @buf filled in.
309 fill_in_wimoverlay_dat(u8 *buf,
310 const struct WimOverlay_dat_header *old_hdr,
311 const wchar_t *wim_path,
312 const u8 wim_guid[WIM_GUID_LEN],
314 u64 new_data_source_id,
315 const PARTITION_INFORMATION_EX *part_info,
316 const DRIVE_LAYOUT_INFORMATION_EX *disk_info,
317 u32 new_entry_2_size)
319 struct WimOverlay_dat_header *new_hdr;
320 struct WimOverlay_dat_entry_1 *new_entry_1;
321 struct WimOverlay_dat_entry_2 *new_entry_2;
325 new_hdr = (struct WimOverlay_dat_header *)p;
327 /* Fill in new header */
328 new_hdr->magic = WIMOVERLAY_DAT_MAGIC;
329 new_hdr->wim_provider_version = WIM_PROVIDER_CURRENT_VERSION;
330 new_hdr->unknown_0x08 = 0x00000028;
331 new_hdr->num_entries_1 = (old_hdr ? old_hdr->num_entries_1 : 0) + 1;
332 new_hdr->num_entries_2 = (old_hdr ? old_hdr->num_entries_2 : 0) + 1;
333 new_hdr->unknown_0x14 = 0x00000000;
335 p += sizeof(struct WimOverlay_dat_header);
337 /* Copy WIM-specific information for old entries */
338 entry_2_offset = sizeof(struct WimOverlay_dat_header) +
339 (new_hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1));
341 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
342 new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
344 p = mempcpy(p, &old_hdr->entry_1s[i],
345 sizeof(struct WimOverlay_dat_entry_1));
347 new_entry_1->entry_2_offset = entry_2_offset;
348 entry_2_offset += new_entry_1->entry_2_length;
352 /* Generate WIM-specific information for new entry */
353 new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
355 new_entry_1->data_source_id = new_data_source_id;
356 new_entry_1->entry_2_offset = entry_2_offset;
357 new_entry_1->entry_2_length = new_entry_2_size;
358 new_entry_1->wim_type = WIM_BOOT_NOT_OS_WIM;
359 new_entry_1->wim_index = image;
360 BUILD_BUG_ON(sizeof(new_entry_1->guid) != WIM_GUID_LEN);
361 memcpy(new_entry_1->guid, wim_guid, WIM_GUID_LEN);
363 p += sizeof(struct WimOverlay_dat_entry_1);
365 /* Copy WIM location information for old entries */
367 for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
368 wimlib_assert(new_hdr->entry_1s[i].entry_2_offset == p - buf);
369 wimlib_assert(old_hdr->entry_1s[i].entry_2_length ==
370 new_hdr->entry_1s[i].entry_2_length);
372 ((const u8 *)old_hdr + old_hdr->entry_1s[i].entry_2_offset),
373 old_hdr->entry_1s[i].entry_2_length);
377 /* Generate WIM location information for new entry */
378 new_entry_2 = (struct WimOverlay_dat_entry_2 *)p;
380 new_entry_2->unknown_0x00 = 0x00000000;
381 new_entry_2->unknown_0x04 = 0x00000000;
382 new_entry_2->entry_2_length = new_entry_2_size;
383 new_entry_2->unknown_0x0C = 0x00000000;
384 new_entry_2->unknown_0x10 = 0x00000005;
385 new_entry_2->unknown_0x14 = 0x00000001;
386 new_entry_2->inner_struct_size = new_entry_2_size - 0x14;
387 new_entry_2->unknown_0x1C = 0x00000005;
388 new_entry_2->unknown_0x20 = 0x00000006;
389 new_entry_2->unknown_0x24 = 0x00000000;
390 new_entry_2->unknown_0x28 = 0x00000048;
391 new_entry_2->unknown_0x2C = 0x00000000;
392 new_entry_2->unknown_0x40 = 0x00000000;
394 if (part_info->PartitionStyle == PARTITION_STYLE_MBR) {
395 new_entry_2->partition.mbr.part_start_offset = part_info->StartingOffset.QuadPart;
396 new_entry_2->partition.mbr.padding = 0;
397 new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_MBR;
398 new_entry_2->disk.mbr.disk_id = disk_info->Mbr.Signature;
399 new_entry_2->disk.mbr.padding[0] = 0x00000000;
400 new_entry_2->disk.mbr.padding[1] = 0x00000000;
401 new_entry_2->disk.mbr.padding[2] = 0x00000000;
403 BUILD_BUG_ON(sizeof(new_entry_2->partition.gpt.part_unique_guid) !=
404 sizeof(part_info->Gpt.PartitionId));
405 memcpy(new_entry_2->partition.gpt.part_unique_guid,
406 &part_info->Gpt.PartitionId,
407 sizeof(part_info->Gpt.PartitionId));
408 new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_GPT;
410 BUILD_BUG_ON(sizeof(new_entry_2->disk.gpt.disk_guid) !=
411 sizeof(disk_info->Gpt.DiskId));
412 memcpy(new_entry_2->disk.gpt.disk_guid,
413 &disk_info->Gpt.DiskId,
414 sizeof(disk_info->Gpt.DiskId));
416 BUILD_BUG_ON(sizeof(new_entry_2->disk.gpt.disk_guid) !=
417 sizeof(new_entry_2->partition.gpt.part_unique_guid));
419 new_entry_2->unknown_0x58[0] = 0x00000000;
420 new_entry_2->unknown_0x58[1] = 0x00000000;
421 new_entry_2->unknown_0x58[2] = 0x00000000;
422 new_entry_2->unknown_0x58[3] = 0x00000000;
424 wimlib_assert(wim_path[2] == L'\\');
425 return mempcpy(new_entry_2->wim_file_name,
427 new_entry_2_size - sizeof(struct WimOverlay_dat_entry_2));
431 * Prepares the new contents of WimOverlay.dat in memory, with one entry added.
434 * Old contents of WimOverlay.dat, or NULL if it did not exist.
436 * Absolute path to the WIM file. It must begin with a drive letter; for
437 * example, D:\install.wim.
439 * GUID of the WIM, from the WIM header.
441 * Number of the image in the WIM to specify in the new entry.
443 * Location into which to return the new contents as a malloc()ed buffer on
445 * @new_contents_size_ret
446 * Location into which to return the size, in bytes, of the new contents on
448 * @new_data_source_id_ret
449 * Location into which to return the data source ID of the new entry on
452 * Returns 0 on success, or a positive error code on failure.
455 prepare_wimoverlay_dat(const struct WimOverlay_dat_header *old_hdr,
456 const wchar_t *wim_path,
457 const u8 wim_guid[WIM_GUID_LEN],
459 void **new_contents_ret,
460 u32 *new_contents_size_ret,
461 u64 *new_data_source_id_ret)
464 PARTITION_INFORMATION_EX part_info;
465 DRIVE_LAYOUT_INFORMATION_EX disk_info;
466 u64 new_data_source_id;
467 u32 new_entry_2_size;
468 u32 new_contents_size;
472 ret = query_partition_and_disk_info(wim_path, &part_info, &disk_info);
476 new_data_source_id = alloc_new_data_source_id(old_hdr);
478 new_entry_2_size = sizeof(struct WimOverlay_dat_entry_2) +
479 ((wcslen(wim_path) - 2 + 1) * sizeof(wchar_t));
480 ret = calculate_wimoverlay_dat_size(old_hdr, new_entry_2_size,
485 buf = MALLOC(new_contents_size);
487 return WIMLIB_ERR_NOMEM;
489 end = fill_in_wimoverlay_dat(buf, old_hdr, wim_path, wim_guid, image,
491 &part_info, &disk_info, new_entry_2_size);
493 wimlib_assert(end - buf == new_contents_size);
495 *new_contents_ret = buf;
496 *new_contents_size_ret = new_contents_size;
497 *new_data_source_id_ret = new_data_source_id;
502 * Reads and validates a WimOverlay.dat file.
505 * Path to the WimOverlay.dat file, such as
506 * C:\System Volume Information\WimOverlay.dat
508 * Location into which to return the contents as a malloc()ed buffer on
509 * success. This can be cast to 'struct WimOverlay_dat_header', and its
510 * contents are guaranteed to be valid. Alternatively, if the file does
511 * not exist, NULL will be returned here.
513 * Returns 0 on success, or a positive error code on failure.
516 read_wimoverlay_dat(const wchar_t *path, void **contents_ret)
519 BY_HANDLE_FILE_INFORMATION info;
522 const struct WimOverlay_dat_header *hdr;
524 bool already_retried = false;
527 h = open_file(path, GENERIC_READ);
528 if (h == INVALID_HANDLE_VALUE) {
529 DWORD err = GetLastError();
530 if (err == ERROR_FILE_NOT_FOUND) {
531 *contents_ret = NULL;
534 if (err == ERROR_PATH_NOT_FOUND &&
535 func_RtlCreateSystemVolumeInformationFolder)
537 wchar_t volume_root_path[] = L"\\??\\X:\\";
539 *(wcschr(volume_root_path, L'X')) = path[0];
541 UNICODE_STRING str = {
542 .Length = sizeof(volume_root_path) - sizeof(wchar_t),
543 .MaximumLength = sizeof(volume_root_path),
544 .Buffer = volume_root_path,
549 status = (*func_RtlCreateSystemVolumeInformationFolder)(&str);
551 err2 = (*func_RtlNtStatusToDosError)(status);
552 if (err2 == ERROR_SUCCESS) {
553 if (!already_retried) {
554 already_retried = true;
561 set_errno_from_win32_error(err);
562 ERROR_WITH_ERRNO("\"%ls\": Can't open for reading", path);
563 return WIMLIB_ERR_OPEN;
565 if (!GetFileInformationByHandle(h, &info)) {
566 set_errno_from_GetLastError();
567 ERROR_WITH_ERRNO("\"%ls\": Can't query metadata", path);
569 return WIMLIB_ERR_STAT;
573 if (!info.nFileSizeHigh)
574 contents = MALLOC(info.nFileSizeLow);
576 ERROR("\"%ls\": File is too large to fit into memory", path);
578 return WIMLIB_ERR_NOMEM;
581 if (!ReadFile(h, contents, info.nFileSizeLow, &bytes_read, NULL) ||
582 bytes_read != info.nFileSizeLow)
584 set_errno_from_GetLastError();
585 ERROR_WITH_ERRNO("\"%ls\": Can't read data", path);
587 ret = WIMLIB_ERR_READ;
588 goto out_free_contents;
593 if (info.nFileSizeLow < sizeof(struct WimOverlay_dat_header)) {
594 ERROR("\"%ls\": File is unexpectedly small (only %"PRIu32" bytes)",
595 path, (u32)info.nFileSizeLow);
596 ret = WIMLIB_ERR_UNSUPPORTED;
597 goto out_free_contents;
600 hdr = (const struct WimOverlay_dat_header *)contents;
602 if (hdr->magic != WIMOVERLAY_DAT_MAGIC ||
603 hdr->wim_provider_version != WIM_PROVIDER_CURRENT_VERSION ||
604 hdr->unknown_0x08 != 0x00000028 ||
605 (hdr->num_entries_1 != hdr->num_entries_2) ||
606 hdr->unknown_0x14 != 0x00000000)
608 ERROR("\"%ls\": Header contains unexpected data:", path);
609 if (wimlib_print_errors) {
610 print_byte_field((const u8 *)hdr,
611 sizeof(struct WimOverlay_dat_header),
615 ret = WIMLIB_ERR_UNSUPPORTED;
616 goto out_free_contents;
619 if ((u64)hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1) >
620 info.nFileSizeLow - sizeof(struct WimOverlay_dat_header))
622 ERROR("\"%ls\": File is unexpectedly small "
623 "(only %"PRIu32" bytes, but has %"PRIu32" entries)",
624 path, (u32)info.nFileSizeLow, hdr->num_entries_1);
625 ret = WIMLIB_ERR_UNSUPPORTED;
626 goto out_free_contents;
629 for (u32 i = 0; i < hdr->num_entries_1; i++) {
630 const struct WimOverlay_dat_entry_1 *entry_1;
631 const struct WimOverlay_dat_entry_2 *entry_2;
632 u32 wim_file_name_length;
634 entry_1 = &hdr->entry_1s[i];
636 if (((u64)entry_1->entry_2_offset +
637 (u64)entry_1->entry_2_length) >
640 ERROR("\"%ls\": entry %"PRIu32" (2) "
641 "(data source ID 0x%016"PRIx64") "
643 path, i, entry_1->data_source_id);
644 ret = WIMLIB_ERR_UNSUPPORTED;
645 goto out_free_contents;
647 if (entry_1->entry_2_length < sizeof(struct WimOverlay_dat_entry_2)) {
648 ERROR("\"%ls\": entry %"PRIu32" (2) "
649 "(data source ID 0x%016"PRIx64") "
651 path, i, entry_1->data_source_id);
652 ret = WIMLIB_ERR_UNSUPPORTED;
653 goto out_free_contents;
656 if (entry_1->entry_2_offset % 2 != 0) {
657 ERROR("\"%ls\": entry %"PRIu32" (2) "
658 "(data source ID 0x%016"PRIx64") "
660 path, i, entry_1->data_source_id);
661 ret = WIMLIB_ERR_UNSUPPORTED;
662 goto out_free_contents;
665 entry_2 = (const struct WimOverlay_dat_entry_2 *)
666 ((const u8 *)hdr + entry_1->entry_2_offset);
668 wim_file_name_length = entry_1->entry_2_length -
669 sizeof(struct WimOverlay_dat_entry_2);
670 if ((wim_file_name_length < 2 * sizeof(wchar_t)) ||
671 (wim_file_name_length % sizeof(wchar_t) != 0) ||
672 (wmemchr(entry_2->wim_file_name, L'\0',
673 wim_file_name_length / sizeof(wchar_t))
674 != &entry_2->wim_file_name[wim_file_name_length /
675 sizeof(wchar_t) - 1]))
677 ERROR("\"%ls\": entry %"PRIu32" (2) "
678 "(data source ID 0x%016"PRIx64") "
679 "has invalid WIM file name",
680 path, i, entry_1->data_source_id);
681 if (wimlib_print_errors) {
682 print_byte_field((const u8 *)entry_2->wim_file_name,
683 wim_file_name_length, stderr);
686 ret = WIMLIB_ERR_UNSUPPORTED;
687 goto out_free_contents;
690 if (entry_2->unknown_0x00 != 0x00000000 ||
691 entry_2->unknown_0x04 != 0x00000000 ||
692 entry_2->unknown_0x0C != 0x00000000 ||
693 entry_2->entry_2_length != entry_1->entry_2_length ||
694 entry_2->unknown_0x10 != 0x00000005 ||
695 entry_2->unknown_0x14 != 0x00000001 ||
696 entry_2->inner_struct_size != entry_1->entry_2_length - 0x14 ||
697 entry_2->unknown_0x1C != 0x00000005 ||
698 entry_2->unknown_0x20 != 0x00000006 ||
699 entry_2->unknown_0x24 != 0x00000000 ||
700 entry_2->unknown_0x28 != 0x00000048 ||
701 entry_2->unknown_0x2C != 0x00000000 ||
702 entry_2->unknown_0x40 != 0x00000000 ||
703 (entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_GPT &&
704 entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_MBR) ||
705 (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_MBR &&
706 entry_2->partition.mbr.padding != 0) ||
707 (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_GPT &&
708 entry_2->partition.mbr.padding == 0) ||
709 entry_2->unknown_0x58[0] != 0x00000000 ||
710 entry_2->unknown_0x58[1] != 0x00000000 ||
711 entry_2->unknown_0x58[2] != 0x00000000 ||
712 entry_2->unknown_0x58[3] != 0x00000000)
714 ERROR("\"%ls\": entry %"PRIu32" (2) "
715 "(data source ID 0x%016"PRIx64") "
716 "contains unexpected data!",
717 path, i, entry_1->data_source_id);
718 if (wimlib_print_errors) {
719 print_byte_field((const u8 *)entry_2,
720 entry_1->entry_2_length,
724 ret = WIMLIB_ERR_UNSUPPORTED;
725 goto out_free_contents;
729 *contents_ret = contents;
738 * Update WimOverlay.dat manually in order to add a WIM data source to the
741 * THIS CODE IS EXPERIMENTAL AS I HAD TO REVERSE ENGINEER THE FILE FORMAT!
744 * Target drive. Must be a letter followed by a colon (e.g. D:).
746 * Absolute path to the WIM file. It must begin with a drive letter; for
747 * example, D:\install.wim.
749 * GUID of the WIM, from the WIM header.
751 * Number of the image in the WIM to specify in the new entry.
752 * @data_source_id_ret
753 * On success, the allocated data source ID is returned here.
756 update_wimoverlay_manually(const wchar_t *drive, const wchar_t *wim_path,
757 const u8 wim_guid[WIM_GUID_LEN],
758 int image, u64 *data_source_id_ret)
760 wchar_t path_main[] = L"A:\\System Volume Information\\WimOverlay.dat";
761 wchar_t path_backup[] = L"A:\\System Volume Information\\WimOverlay.backup";
762 wchar_t path_wimlib_backup[] = L"A:\\System Volume Information\\WimOverlay.wimlib_backup";
763 wchar_t path_new[] = L"A:\\System Volume Information\\WimOverlay.wimlib_new";
766 u32 new_contents_size;
767 u64 new_data_source_id;
770 wimlib_assert(drive[0] != L'\0' &&
774 path_main[0] = drive[0];
775 path_backup[0] = drive[0];
776 path_wimlib_backup[0] = drive[0];
777 path_new[0] = drive[0];
779 ret = read_wimoverlay_dat(path_main, &old_contents);
783 ret = prepare_wimoverlay_dat(old_contents, wim_path, wim_guid, image,
784 &new_contents, &new_contents_size,
785 &new_data_source_id);
790 /* Write WimOverlay.wimlib_new */
791 ret = write_wimoverlay_dat(path_new,
792 new_contents, new_contents_size);
794 goto out_free_new_contents;
796 /* Write WimOverlay.backup */
797 ret = write_wimoverlay_dat(path_backup,
798 new_contents, new_contents_size);
800 goto out_free_new_contents;
803 /* Rename WimOverlay.dat => WimOverlay.wimlib_backup */
804 ret = win32_rename_replacement(path_main, path_wimlib_backup);
806 ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
807 path_main, path_wimlib_backup);
808 goto out_free_new_contents;
812 /* Rename WimOverlay.wimlib_new => WimOverlay.dat */
813 ret = win32_rename_replacement(path_new, path_main);
815 ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
816 path_new, path_main);
818 out_free_new_contents:
821 if (ret == WIMLIB_ERR_UNSUPPORTED) {
822 ERROR("Please report to developer ("PACKAGE_BUGREPORT").\n"
823 " If possible send the file \"%ls\".\n\n", path_main);
826 *data_source_id_ret = new_data_source_id;
831 win32_get_drive_path(const wchar_t *file_path, wchar_t drive_path[7])
835 file_abspath = realpath(file_path, NULL);
837 return WIMLIB_ERR_NOMEM;
839 if (file_abspath[0] == L'\0' || file_abspath[1] != L':') {
840 ERROR("\"%ls\": Path format not recognized", file_abspath);
842 return WIMLIB_ERR_UNSUPPORTED;
845 wsprintf(drive_path, L"\\\\.\\%lc:", file_abspath[0]);
850 /* Try to attach an instance of the Windows Overlay File System Filter Driver to
851 * the specified drive (such as C:) */
853 try_to_attach_wof(const wchar_t *drive)
858 /* Use FilterAttach() from Fltlib.dll. */
860 fltlib = LoadLibrary(L"Fltlib.dll");
863 WARNING("Failed to load Fltlib.dll");
867 HRESULT (WINAPI *func_FilterAttach)(LPCWSTR lpFilterName,
868 LPCWSTR lpVolumeName,
869 LPCWSTR lpInstanceName,
870 DWORD dwCreatedInstanceNameLength,
871 LPWSTR lpCreatedInstanceName);
873 func_FilterAttach = (void *)GetProcAddress(fltlib, "FilterAttach");
875 if (func_FilterAttach) {
878 res = (*func_FilterAttach)(L"WoF", drive, NULL, 0, NULL);
883 WARNING("FilterAttach() does not exist in Fltlib.dll");
892 * Allocate a WOF data source ID for a WIM file.
895 * Absolute path to the WIM file. This must include a drive letter and use
896 * backslash path separators.
898 * GUID of the WIM, from the WIM header.
900 * Number of the image in the WIM being applied.
902 * Path to the target directory.
903 * @data_source_id_ret
904 * On success, an identifier for the backing WIM file will be returned
907 * Returns 0 on success, or a positive error code on failure.
910 wimboot_alloc_data_source_id(const wchar_t *wim_path,
911 const u8 wim_guid[WIM_GUID_LEN],
912 int image, const wchar_t *target,
913 u64 *data_source_id_ret, bool *wof_running_ret)
916 size_t wim_path_nchars;
917 size_t wim_file_name_length;
920 struct wof_external_info *wof_info;
921 struct wim_provider_add_overlay_input *wim_info;
924 DWORD bytes_returned;
926 const wchar_t *prefix = L"\\??\\";
927 const size_t prefix_nchars = 4;
928 bool tried_to_attach_wof = false;
930 ret = win32_get_drive_path(target, drive_path);
934 wimlib_assert(!wcschr(wim_path, L'/'));
935 wimlib_assert(wim_path[0] != L'\0' && wim_path[1] == L':');
937 wim_path_nchars = wcslen(wim_path);
938 wim_file_name_length = sizeof(wchar_t) *
939 (wim_path_nchars + prefix_nchars);
941 insize = sizeof(struct wof_external_info) +
942 sizeof(struct wim_provider_add_overlay_input) +
943 wim_file_name_length;
947 ret = WIMLIB_ERR_NOMEM;
951 wof_info = (struct wof_external_info *)in;
952 wof_info->version = WOF_CURRENT_VERSION;
953 wof_info->provider = WOF_PROVIDER_WIM;
955 wim_info = (struct wim_provider_add_overlay_input *)(wof_info + 1);
956 wim_info->wim_type = WIM_BOOT_NOT_OS_WIM;
957 wim_info->wim_index = image;
958 wim_info->wim_file_name_offset = offsetof(struct wim_provider_add_overlay_input,
960 wim_info->wim_file_name_length = wim_file_name_length;
961 wmemcpy(&wim_info->wim_file_name[0], prefix, prefix_nchars);
962 wmemcpy(&wim_info->wim_file_name[prefix_nchars], wim_path, wim_path_nchars);
965 h = open_file(drive_path, GENERIC_WRITE);
967 if (h == INVALID_HANDLE_VALUE) {
968 set_errno_from_GetLastError();
969 ERROR_WITH_ERRNO("Failed to open \"%ls\"", drive_path + 4);
970 ret = WIMLIB_ERR_OPEN;
974 if (!DeviceIoControl(h, FSCTL_ADD_OVERLAY,
976 &data_source_id, sizeof(data_source_id),
977 &bytes_returned, NULL))
979 DWORD err = GetLastError();
980 if (err == ERROR_INVALID_FUNCTION) {
981 if (!tried_to_attach_wof) {
983 h = INVALID_HANDLE_VALUE;
984 tried_to_attach_wof = true;
985 if (try_to_attach_wof(drive_path + 4))
988 ret = WIMLIB_ERR_UNSUPPORTED;
989 goto out_close_handle;
991 set_errno_from_win32_error(err);
992 ERROR_WITH_ERRNO("Failed to add overlay source \"%ls\" "
993 "to volume \"%ls\" (err=0x%08"PRIx32")",
994 wim_path, drive_path + 4, (u32)err);
995 ret = WIMLIB_ERR_WIMBOOT;
996 goto out_close_handle;
1000 if (bytes_returned != sizeof(data_source_id)) {
1001 set_errno_from_win32_error(ERROR_INVALID_DATA);
1002 ret = WIMLIB_ERR_WIMBOOT;
1003 ERROR("Unexpected result size when adding "
1004 "overlay source \"%ls\" to volume \"%ls\"",
1005 wim_path, drive_path + 4);
1006 goto out_close_handle;
1009 *wof_running_ret = true;
1010 *data_source_id_ret = data_source_id;
1018 if (ret == WIMLIB_ERR_UNSUPPORTED) {
1021 "The version of Windows you are running does not appear to support\n"
1022 " the Windows Overlay File System Filter Driver. This is normally\n"
1023 " available on Windows 8.1 Update 1 or later. Therefore, wimlib\n"
1024 " will attempt to update the WimOverlay.dat file directly.\n");
1026 WARNING("WOF driver is not available; updating WimOverlay.dat directly.");
1028 ret = update_wimoverlay_manually(drive_path + 4, wim_path,
1030 data_source_id_ret);
1031 *wof_running_ret = false;
1038 * Set WIMBoot information on the specified file.
1040 * This turns it into a reparse point that redirects accesses to it, to the
1041 * corresponding resource in the WIM archive.
1044 * Object attributes that specify the path to the file.
1046 * Printable representation of the path encoded in @attr.
1048 * Unnamed data stream of the file.
1050 * Allocated identifier for the WIM data source on the destination volume.
1051 * @lookup_table_hash
1052 * SHA-1 message digest of the WIM's lookup table.
1054 * %true if the WOF driver appears to be available and working; %false if
1057 * Returns 0 on success, or a positive error code on failure.
1060 wimboot_set_pointer(OBJECT_ATTRIBUTES *attr,
1061 const wchar_t *printable_name,
1062 const struct wim_lookup_table_entry *lte,
1064 const u8 lookup_table_hash[SHA1_HASH_SIZE],
1070 IO_STATUS_BLOCK iosb;
1071 DWORD bytes_returned;
1074 status = (*func_NtOpenFile)(&h, GENERIC_WRITE | SYNCHRONIZE, attr,
1075 &iosb, FILE_SHARE_VALID_FLAGS,
1076 FILE_OPEN_FOR_BACKUP_INTENT |
1077 FILE_OPEN_REPARSE_POINT |
1078 FILE_SYNCHRONOUS_IO_NONALERT);
1079 if (!NT_SUCCESS(status)) {
1080 SetLastError((*func_RtlNtStatusToDosError)(status));
1085 /* The WOF driver is running. We can create the reparse point
1086 * using FSCTL_SET_EXTERNAL_BACKING. */
1089 struct wof_external_info wof_info;
1090 struct wim_provider_external_info wim_info;
1093 in.wof_info.version = WOF_CURRENT_VERSION;
1094 in.wof_info.provider = WOF_PROVIDER_WIM;
1096 in.wim_info.version = WIM_PROVIDER_CURRENT_VERSION;
1097 in.wim_info.flags = 0;
1098 in.wim_info.data_source_id = data_source_id;
1099 copy_hash(in.wim_info.resource_hash, lte->hash);
1101 /* lookup_table_hash is not necessary */
1103 if (!DeviceIoControl(h, FSCTL_SET_EXTERNAL_BACKING,
1104 &in, sizeof(in), NULL, 0,
1105 &bytes_returned, NULL))
1109 /* The WOF driver is running. We need to create the reparse
1110 * point manually. */
1118 struct wof_external_info wof_info;
1119 struct wim_provider_rpdata wim_info;
1122 BUILD_BUG_ON(sizeof(in) != 8 +
1123 sizeof(struct wof_external_info) +
1124 sizeof(struct wim_provider_rpdata));
1126 in.hdr.rptag = WIMLIB_REPARSE_TAG_WOF;
1127 in.hdr.rpdatalen = sizeof(in) - sizeof(in.hdr);
1128 in.hdr.rpreserved = 0;
1130 in.wof_info.version = WOF_CURRENT_VERSION;
1131 in.wof_info.provider = WOF_PROVIDER_WIM;
1133 in.wim_info.version = 2;
1134 in.wim_info.flags = 0;
1135 in.wim_info.data_source_id = data_source_id;
1136 copy_hash(in.wim_info.resource_hash, lte->hash);
1137 copy_hash(in.wim_info.wim_lookup_table_hash, lookup_table_hash);
1138 in.wim_info.stream_uncompressed_size = lte->size;
1139 in.wim_info.stream_compressed_size = lte->rspec->size_in_wim;
1140 in.wim_info.stream_offset_in_wim = lte->rspec->offset_in_wim;
1142 if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
1143 &in, sizeof(in), NULL, 0, &bytes_returned, NULL))
1146 /* We also need to create an unnamed data stream of the correct
1147 * size. Otherwise the file shows up as zero length. It can be
1148 * a sparse stream containing all zeroes; its contents
1149 * are unimportant. */
1150 if (!DeviceIoControl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0,
1151 &bytes_returned, NULL))
1154 if (!SetFilePointerEx(h,
1155 (LARGE_INTEGER){ .QuadPart = lte->size},
1159 if (!SetEndOfFile(h))
1167 err = GetLastError();
1168 set_errno_from_win32_error(err);
1169 ERROR_WITH_ERRNO("\"%ls\": Couldn't set WIMBoot pointer data "
1170 "(err=%"PRIu32")", printable_name, (u32)err);
1171 ret = WIMLIB_ERR_WIMBOOT;