+/*
+ * Generates the contents of WimOverlay.dat in memory, with one entry added.
+ *
+ * @buf
+ * Buffer large enough to hold the new contents.
+ * @old_hdr
+ * Old contents of WimOverlay.dat, or NULL if it did not exist.
+ * @wim_path
+ * Absolute path to the WIM file. It must begin with a drive letter; for
+ * example, D:\install.wim.
+ * @wim_guid
+ * GUID of the WIM, from the WIM header.
+ * @image
+ * Number of the image in the WIM to specify in the new entry.
+ * @new_data_source_id
+ * Data source ID to use for the new entry.
+ * @part_info
+ * Partition information for the WIM file.
+ * @disk_info
+ * Disk information for the WIM file.
+ * @new_entry_2_size
+ * Size, in bytes, of the new location information structure ('struct
+ * WimOverlay_dat_entry_2').
+ *
+ * Returns a pointer one past the last byte of @buf filled in.
+ */
+static u8 *
+fill_in_wimoverlay_dat(u8 *buf,
+ const struct WimOverlay_dat_header *old_hdr,
+ const wchar_t *wim_path,
+ const u8 wim_guid[GUID_SIZE],
+ int image,
+ u64 new_data_source_id,
+ const PARTITION_INFORMATION_EX *part_info,
+ const DRIVE_LAYOUT_INFORMATION_EX *disk_info,
+ u32 new_entry_2_size)
+{
+ struct WimOverlay_dat_header *new_hdr;
+ struct WimOverlay_dat_entry_1 *new_entry_1;
+ struct WimOverlay_dat_entry_2 *new_entry_2;
+ u32 entry_2_offset;
+ u8 *p = buf;
+
+ new_hdr = (struct WimOverlay_dat_header *)p;
+
+ /* Fill in new header */
+ new_hdr->magic = WIMOVERLAY_DAT_MAGIC;
+ new_hdr->wim_provider_version = WIM_PROVIDER_CURRENT_VERSION;
+ new_hdr->unknown_0x08 = 0x00000028;
+ new_hdr->num_entries_1 = (old_hdr ? old_hdr->num_entries_1 : 0) + 1;
+ new_hdr->num_entries_2 = (old_hdr ? old_hdr->num_entries_2 : 0) + 1;
+ new_hdr->unknown_0x14 = 0x00000000;
+
+ p += sizeof(struct WimOverlay_dat_header);
+
+ /* Copy WIM-specific information for old entries */
+ entry_2_offset = sizeof(struct WimOverlay_dat_header) +
+ (new_hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1));
+ if (old_hdr) {
+ for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
+ new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
+
+ p = mempcpy(p, &old_hdr->entry_1s[i],
+ sizeof(struct WimOverlay_dat_entry_1));
+
+ new_entry_1->entry_2_offset = entry_2_offset;
+ entry_2_offset += new_entry_1->entry_2_length;
+ }
+ }
+
+ /* Generate WIM-specific information for new entry */
+ new_entry_1 = (struct WimOverlay_dat_entry_1 *)p;
+
+ new_entry_1->data_source_id = new_data_source_id;
+ new_entry_1->entry_2_offset = entry_2_offset;
+ new_entry_1->entry_2_length = new_entry_2_size;
+ new_entry_1->wim_type = WIM_BOOT_NOT_OS_WIM;
+ new_entry_1->wim_index = image;
+ STATIC_ASSERT(sizeof(new_entry_1->guid) == GUID_SIZE);
+ copy_guid(new_entry_1->guid, wim_guid);
+
+ p += sizeof(struct WimOverlay_dat_entry_1);
+
+ /* Copy WIM location information for old entries */
+ if (old_hdr) {
+ for (u32 i = 0; i < old_hdr->num_entries_1; i++) {
+ wimlib_assert(new_hdr->entry_1s[i].entry_2_offset == p - buf);
+ wimlib_assert(old_hdr->entry_1s[i].entry_2_length ==
+ new_hdr->entry_1s[i].entry_2_length);
+ p = mempcpy(p,
+ ((const u8 *)old_hdr + old_hdr->entry_1s[i].entry_2_offset),
+ old_hdr->entry_1s[i].entry_2_length);
+ }
+ }
+
+ /* Generate WIM location information for new entry */
+ new_entry_2 = (struct WimOverlay_dat_entry_2 *)p;
+
+ new_entry_2->unknown_0x00 = 0x00000000;
+ new_entry_2->unknown_0x04 = 0x00000000;
+ new_entry_2->entry_2_length = new_entry_2_size;
+ new_entry_2->unknown_0x0C = 0x00000000;
+ new_entry_2->unknown_0x10 = 0x00000005;
+ new_entry_2->unknown_0x14 = 0x00000001;
+ new_entry_2->inner_struct_size = new_entry_2_size - 0x14;
+ new_entry_2->unknown_0x1C = 0x00000005;
+ new_entry_2->unknown_0x20 = 0x00000006;
+ new_entry_2->unknown_0x24 = 0x00000000;
+ new_entry_2->unknown_0x28 = 0x00000048;
+ new_entry_2->unknown_0x2C = 0x00000000;
+ new_entry_2->unknown_0x40 = 0x00000000;
+
+ if (part_info->PartitionStyle == PARTITION_STYLE_MBR) {
+ new_entry_2->partition.mbr.part_start_offset = part_info->StartingOffset.QuadPart;
+ new_entry_2->partition.mbr.padding = 0;
+ new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_MBR;
+ new_entry_2->disk.mbr.disk_id = disk_info->Mbr.Signature;
+ new_entry_2->disk.mbr.padding[0] = 0x00000000;
+ new_entry_2->disk.mbr.padding[1] = 0x00000000;
+ new_entry_2->disk.mbr.padding[2] = 0x00000000;
+ } else {
+ STATIC_ASSERT(sizeof(new_entry_2->partition.gpt.part_unique_guid) ==
+ sizeof(part_info->Gpt.PartitionId));
+ memcpy(new_entry_2->partition.gpt.part_unique_guid,
+ &part_info->Gpt.PartitionId,
+ sizeof(part_info->Gpt.PartitionId));
+ new_entry_2->partition_table_type = WIMOVERLAY_PARTITION_TYPE_GPT;
+
+ STATIC_ASSERT(sizeof(new_entry_2->disk.gpt.disk_guid) ==
+ sizeof(disk_info->Gpt.DiskId));
+ memcpy(new_entry_2->disk.gpt.disk_guid,
+ &disk_info->Gpt.DiskId,
+ sizeof(disk_info->Gpt.DiskId));
+
+ STATIC_ASSERT(sizeof(new_entry_2->disk.gpt.disk_guid) ==
+ sizeof(new_entry_2->partition.gpt.part_unique_guid));
+ }
+ new_entry_2->unknown_0x58[0] = 0x00000000;
+ new_entry_2->unknown_0x58[1] = 0x00000000;
+ new_entry_2->unknown_0x58[2] = 0x00000000;
+ new_entry_2->unknown_0x58[3] = 0x00000000;
+
+ wimlib_assert(wim_path[2] == L'\\');
+ return mempcpy(new_entry_2->wim_file_name,
+ wim_path + 2,
+ new_entry_2_size - sizeof(struct WimOverlay_dat_entry_2));
+}
+
+/*
+ * Prepares the new contents of WimOverlay.dat in memory, with one entry added.
+ *
+ * @old_hdr
+ * Old contents of WimOverlay.dat, or NULL if it did not exist.
+ * @wim_path
+ * Absolute path to the WIM file. It must begin with a drive letter; for
+ * example, D:\install.wim.
+ * @wim_guid
+ * GUID of the WIM, from the WIM header.
+ * @image
+ * Number of the image in the WIM to specify in the new entry.
+ * @new_contents_ret
+ * Location into which to return the new contents as a malloc()ed buffer on
+ * success.
+ * @new_contents_size_ret
+ * Location into which to return the size, in bytes, of the new contents on
+ * success.
+ * @new_data_source_id_ret
+ * Location into which to return the data source ID of the new entry on
+ * success.
+ *
+ * Returns 0 on success, or a positive error code on failure.
+ */
+static int
+prepare_wimoverlay_dat(const struct WimOverlay_dat_header *old_hdr,
+ const wchar_t *wim_path,
+ const u8 wim_guid[GUID_SIZE],
+ int image,
+ void **new_contents_ret,
+ u32 *new_contents_size_ret,
+ u64 *new_data_source_id_ret)
+{
+ int ret;
+ PARTITION_INFORMATION_EX part_info;
+ DRIVE_LAYOUT_INFORMATION_EX disk_info;
+ u64 new_data_source_id;
+ u32 new_entry_2_size;
+ u32 new_contents_size;
+ u8 *buf;
+ u8 *end;
+
+ ret = query_partition_and_disk_info(wim_path, &part_info, &disk_info);
+ if (ret)
+ return ret;
+
+ new_data_source_id = alloc_new_data_source_id(old_hdr);
+
+ new_entry_2_size = sizeof(struct WimOverlay_dat_entry_2) +
+ ((wcslen(wim_path) - 2 + 1) * sizeof(wchar_t));
+ ret = calculate_wimoverlay_dat_size(old_hdr, new_entry_2_size,
+ &new_contents_size);
+ if (ret)
+ return ret;
+
+ buf = MALLOC(new_contents_size);
+ if (!buf)
+ return WIMLIB_ERR_NOMEM;
+
+ end = fill_in_wimoverlay_dat(buf, old_hdr, wim_path, wim_guid, image,
+ new_data_source_id,
+ &part_info, &disk_info, new_entry_2_size);
+
+ wimlib_assert(end - buf == new_contents_size);
+
+ *new_contents_ret = buf;
+ *new_contents_size_ret = new_contents_size;
+ *new_data_source_id_ret = new_data_source_id;
+ return 0;
+}
+
+/*
+ * Reads and validates a WimOverlay.dat file.
+ *
+ * @path
+ * Path to the WimOverlay.dat file, such as
+ * C:\System Volume Information\WimOverlay.dat
+ * @contents_ret
+ * Location into which to return the contents as a malloc()ed buffer on
+ * success. This can be cast to 'struct WimOverlay_dat_header', and its
+ * contents are guaranteed to be valid. Alternatively, if the file does
+ * not exist, NULL will be returned here.
+ *
+ * Returns 0 on success, or a positive error code on failure.
+ */
+static int
+read_wimoverlay_dat(const wchar_t *path, void **contents_ret)
+{
+ HANDLE h;
+ BY_HANDLE_FILE_INFORMATION info;
+ int ret;
+ void *contents;
+ const struct WimOverlay_dat_header *hdr;
+ DWORD bytes_read;
+ bool already_retried = false;
+
+retry:
+ h = open_file(path, GENERIC_READ);
+ if (h == INVALID_HANDLE_VALUE) {
+ DWORD err = GetLastError();
+ if (err == ERROR_FILE_NOT_FOUND) {
+ *contents_ret = NULL;
+ return 0;
+ }
+ if (err == ERROR_PATH_NOT_FOUND &&
+ func_RtlCreateSystemVolumeInformationFolder)
+ {
+ wchar_t volume_root_path[] = L"\\??\\X:\\";
+
+ *(wcschr(volume_root_path, L'X')) = path[0];
+
+ UNICODE_STRING str = {
+ .Length = sizeof(volume_root_path) - sizeof(wchar_t),
+ .MaximumLength = sizeof(volume_root_path),
+ .Buffer = volume_root_path,
+ };
+ NTSTATUS status;
+ DWORD err2;
+
+ status = (*func_RtlCreateSystemVolumeInformationFolder)(&str);
+
+ err2 = (*func_RtlNtStatusToDosError)(status);
+ if (err2 == ERROR_SUCCESS) {
+ if (!already_retried) {
+ already_retried = true;
+ goto retry;
+ }
+ } else {
+ err = err2;
+ }
+ }
+ win32_error(err, L"\"%ls\": Can't open for reading", path);
+ return WIMLIB_ERR_OPEN;
+ }
+ if (!GetFileInformationByHandle(h, &info)) {
+ win32_error(GetLastError(), L"\"%ls\": Can't query metadata", path);
+ CloseHandle(h);
+ return WIMLIB_ERR_STAT;
+ }
+
+ contents = NULL;
+ if (!info.nFileSizeHigh)
+ contents = MALLOC(info.nFileSizeLow);
+ if (!contents) {
+ ERROR("\"%ls\": File is too large to fit into memory", path);
+ CloseHandle(h);
+ return WIMLIB_ERR_NOMEM;
+ }
+
+ SetLastError(0);
+ if (!ReadFile(h, contents, info.nFileSizeLow, &bytes_read, NULL) ||
+ bytes_read != info.nFileSizeLow)
+ {
+ win32_error(GetLastError(), L"\"%ls\": Can't read data", path);
+ CloseHandle(h);
+ ret = WIMLIB_ERR_READ;
+ goto out_free_contents;
+ }
+
+ CloseHandle(h);
+
+ if (info.nFileSizeLow < sizeof(struct WimOverlay_dat_header)) {
+ ERROR("\"%ls\": File is unexpectedly small (only %"PRIu32" bytes)",
+ path, (u32)info.nFileSizeLow);
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+
+ hdr = (const struct WimOverlay_dat_header *)contents;
+
+ if (hdr->magic != WIMOVERLAY_DAT_MAGIC ||
+ hdr->wim_provider_version != WIM_PROVIDER_CURRENT_VERSION ||
+ hdr->unknown_0x08 != 0x00000028 ||
+ (hdr->num_entries_1 != hdr->num_entries_2) ||
+ hdr->unknown_0x14 != 0x00000000)
+ {
+ ERROR("\"%ls\": Header contains unexpected data:", path);
+ if (wimlib_print_errors) {
+ print_byte_field((const u8 *)hdr,
+ sizeof(struct WimOverlay_dat_header),
+ wimlib_error_file);
+ fputc('\n', wimlib_error_file);
+ }
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+
+ if ((u64)hdr->num_entries_1 * sizeof(struct WimOverlay_dat_entry_1) >
+ info.nFileSizeLow - sizeof(struct WimOverlay_dat_header))
+ {
+ ERROR("\"%ls\": File is unexpectedly small "
+ "(only %"PRIu32" bytes, but has %"PRIu32" entries)",
+ path, (u32)info.nFileSizeLow, hdr->num_entries_1);
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+
+ for (u32 i = 0; i < hdr->num_entries_1; i++) {
+ const struct WimOverlay_dat_entry_1 *entry_1;
+ const struct WimOverlay_dat_entry_2 *entry_2;
+ u32 wim_file_name_length;
+
+ entry_1 = &hdr->entry_1s[i];
+
+ if (((u64)entry_1->entry_2_offset +
+ (u64)entry_1->entry_2_length) >
+ info.nFileSizeLow)
+ {
+ ERROR("\"%ls\": entry %"PRIu32" (2) "
+ "(data source ID 0x%016"PRIx64") "
+ "overflows file",
+ path, i, entry_1->data_source_id);
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+ if (entry_1->entry_2_length < sizeof(struct WimOverlay_dat_entry_2)) {
+ ERROR("\"%ls\": entry %"PRIu32" (2) "
+ "(data source ID 0x%016"PRIx64") "
+ "is too short",
+ path, i, entry_1->data_source_id);
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+
+ if (entry_1->entry_2_offset % 2 != 0) {
+ ERROR("\"%ls\": entry %"PRIu32" (2) "
+ "(data source ID 0x%016"PRIx64") "
+ "is misaligned",
+ path, i, entry_1->data_source_id);
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+
+ entry_2 = (const struct WimOverlay_dat_entry_2 *)
+ ((const u8 *)hdr + entry_1->entry_2_offset);
+
+ wim_file_name_length = entry_1->entry_2_length -
+ sizeof(struct WimOverlay_dat_entry_2);
+ if ((wim_file_name_length < 2 * sizeof(wchar_t)) ||
+ (wim_file_name_length % sizeof(wchar_t) != 0) ||
+ (wmemchr(entry_2->wim_file_name, L'\0',
+ wim_file_name_length / sizeof(wchar_t))
+ != &entry_2->wim_file_name[wim_file_name_length /
+ sizeof(wchar_t) - 1]))
+ {
+ ERROR("\"%ls\": entry %"PRIu32" (2) "
+ "(data source ID 0x%016"PRIx64") "
+ "has invalid WIM file name",
+ path, i, entry_1->data_source_id);
+ if (wimlib_print_errors) {
+ print_byte_field((const u8 *)entry_2->wim_file_name,
+ wim_file_name_length,
+ wimlib_error_file);
+ fputc('\n', wimlib_error_file);
+ }
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+
+ if (entry_2->unknown_0x00 != 0x00000000 ||
+ entry_2->unknown_0x04 != 0x00000000 ||
+ entry_2->unknown_0x0C != 0x00000000 ||
+ entry_2->entry_2_length != entry_1->entry_2_length ||
+ entry_2->unknown_0x10 != 0x00000005 ||
+ entry_2->unknown_0x14 != 0x00000001 ||
+ entry_2->inner_struct_size != entry_1->entry_2_length - 0x14 ||
+ entry_2->unknown_0x1C != 0x00000005 ||
+ entry_2->unknown_0x20 != 0x00000006 ||
+ entry_2->unknown_0x24 != 0x00000000 ||
+ entry_2->unknown_0x28 != 0x00000048 ||
+ entry_2->unknown_0x2C != 0x00000000 ||
+ entry_2->unknown_0x40 != 0x00000000 ||
+ (entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_GPT &&
+ entry_2->partition_table_type != WIMOVERLAY_PARTITION_TYPE_MBR) ||
+ (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_MBR &&
+ entry_2->partition.mbr.padding != 0) ||
+ (entry_2->partition_table_type == WIMOVERLAY_PARTITION_TYPE_GPT &&
+ entry_2->partition.mbr.padding == 0) ||
+ entry_2->unknown_0x58[0] != 0x00000000 ||
+ entry_2->unknown_0x58[1] != 0x00000000 ||
+ entry_2->unknown_0x58[2] != 0x00000000 ||
+ entry_2->unknown_0x58[3] != 0x00000000)
+ {
+ ERROR("\"%ls\": entry %"PRIu32" (2) "
+ "(data source ID 0x%016"PRIx64") "
+ "contains unexpected data!",
+ path, i, entry_1->data_source_id);
+ if (wimlib_print_errors) {
+ print_byte_field((const u8 *)entry_2,
+ entry_1->entry_2_length,
+ wimlib_error_file);
+ fputc('\n', wimlib_error_file);
+ }
+ ret = WIMLIB_ERR_UNSUPPORTED;
+ goto out_free_contents;
+ }
+ }
+
+ *contents_ret = contents;
+ return 0;
+
+out_free_contents:
+ FREE(contents);
+ return ret;
+}
+
+/*
+ * Update WimOverlay.dat manually in order to add a WIM data source to the
+ * target volume.
+ *
+ * THIS CODE IS EXPERIMENTAL AS I HAD TO REVERSE ENGINEER THE FILE FORMAT!
+ *
+ * @path
+ * Target drive. Must be a letter followed by a colon (e.g. D:).
+ * @wim_path
+ * Absolute path to the WIM file. It must begin with a drive letter; for
+ * example, D:\install.wim.
+ * @wim_guid
+ * GUID of the WIM, from the WIM header.
+ * @image
+ * Number of the image in the WIM to specify in the new entry.
+ * @data_source_id_ret
+ * On success, the allocated data source ID is returned here.
+ */
+static int
+update_wimoverlay_manually(const wchar_t *drive, const wchar_t *wim_path,
+ const u8 wim_guid[GUID_SIZE],
+ int image, u64 *data_source_id_ret)
+{
+ wchar_t path_main[] = L"A:\\System Volume Information\\WimOverlay.dat";
+ wchar_t path_backup[] = L"A:\\System Volume Information\\WimOverlay.backup";
+ wchar_t path_wimlib_backup[] = L"A:\\System Volume Information\\WimOverlay.wimlib_backup";
+ wchar_t path_new[] = L"A:\\System Volume Information\\WimOverlay.wimlib_new";
+ void *old_contents;
+ void *new_contents;
+ u32 new_contents_size;
+ u64 new_data_source_id;
+ int ret;
+
+ wimlib_assert(drive[0] != L'\0' &&
+ drive[1] == L':' &&
+ drive[2] == L'\0');
+
+ path_main[0] = drive[0];
+ path_backup[0] = drive[0];
+ path_wimlib_backup[0] = drive[0];
+ path_new[0] = drive[0];
+
+ ret = read_wimoverlay_dat(path_main, &old_contents);
+ if (ret)
+ goto out;
+
+ ret = prepare_wimoverlay_dat(old_contents, wim_path, wim_guid, image,
+ &new_contents, &new_contents_size,
+ &new_data_source_id);
+ FREE(old_contents);
+ if (ret)
+ goto out;
+
+ /* Write WimOverlay.wimlib_new */
+ ret = write_wimoverlay_dat(path_new,
+ new_contents, new_contents_size);
+ if (ret)
+ goto out_free_new_contents;
+
+ /* Write WimOverlay.backup */
+ ret = write_wimoverlay_dat(path_backup,
+ new_contents, new_contents_size);
+ if (ret)
+ goto out_free_new_contents;
+
+ if (old_contents) {
+ /* Rename WimOverlay.dat => WimOverlay.wimlib_backup */
+ ret = win32_rename_replacement(path_main, path_wimlib_backup);
+ if (ret) {
+ ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
+ path_main, path_wimlib_backup);
+ ret = WIMLIB_ERR_RENAME;
+ goto out_free_new_contents;
+ }
+ }
+
+ /* Rename WimOverlay.wimlib_new => WimOverlay.dat */
+ ret = win32_rename_replacement(path_new, path_main);
+ if (ret) {
+ ERROR_WITH_ERRNO("Can't rename \"%ls\" => \"%ls\"",
+ path_new, path_main);
+ ret = WIMLIB_ERR_RENAME;
+ }
+out_free_new_contents:
+ FREE(new_contents);
+out:
+ if (ret == WIMLIB_ERR_UNSUPPORTED) {
+ ERROR("Please report to developer ("PACKAGE_BUGREPORT").\n"
+ " If possible send the file \"%ls\".\n\n", path_main);
+ }
+ if (ret == 0)
+ *data_source_id_ret = new_data_source_id;
+ return ret;
+}
+