From dab0de7dbe833a44b3e4c20fcd0044f2d878d10d Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 2 Aug 2024 08:41:25 +0100 Subject: dd: add support for writing to physical drives A common use of 'dd' is to copy boot images to removable media. This didn't work on Windows because opening a physical drive failed with the default flags used by 'dd'. If the output file is a physical drive remove incompatible flags. Writing to a physical drive requires elevated privileges and the drive must be offline. Adds 80 bytes. (GitHub issue #435) --- coreutils/dd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/coreutils/dd.c b/coreutils/dd.c index a9ef68fbc..e6b88acf5 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -333,6 +333,17 @@ static void *alloc_buf(size_t size) return xmalloc(size); } +#if ENABLE_PLATFORM_MINGW32 +// Does 'path' refer to a physical drive in the win32 device namespace? +static int is_drive_path(const char *path) +{ + char *s = auto_string(strdup(path)); + + bs_to_slash(s); + return strncasecmp(s, "//./PhysicalDrive", 17) == 0; +} +#endif + int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int dd_main(int argc UNUSED_PARAM, char **argv) { @@ -557,6 +568,12 @@ int dd_main(int argc UNUSED_PARAM, char **argv) bb_error_msg_and_die("O_DIRECT not supported on this platform"); # endif } +#endif +#if ENABLE_PLATFORM_MINGW32 + if (is_drive_path(outfile)) { + // Turn off options not supported by Windows device files. + oflag &= ~(O_CREAT | O_TRUNC); + } #endif xmove_fd(xopen(outfile, oflag), ofd); -- cgit v1.2.3-55-g6feb