Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create usb flash drive from binary image in Qt4
Forum Updated to NodeBB v4.3 + New Features

Create usb flash drive from binary image in Qt4

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Baca48
    wrote on 25 Jul 2012, 10:43 last edited by
    #1

    Hi!

    I need to create a kind of usb flash drive creator. The problem is that it must be multiplatform and have GUI. GUI is not the problem because I'm quite familiar with Qt4. Hovewer, I don't know how to create flash drives from image file.

    On linux I could use dd for that (just invoke it with QProcess but still it's not the ideal solution) because it could make exact copy of binary file (image) to usb drive (e.g. /dev/sdc). How can I achieve that on my own using Qt4? I can't simple copy file by file because I want to create whole disk structure (partition, bootloader etc.). I hope I've explained well what I need.

    The solution must work on Windows and MacOS. Linux users can use simple bash script that uses dd to achieve the same thing.

    Thanks in advance for any help or tips.

    B 1 Reply Last reply 23 Jul 2015, 11:27
    0
    • P Offline
      P Offline
      p.kraszewski
      wrote on 25 Jul 2012, 12:06 last edited by
      #2

      Windows counterparts of whole-disks /dev/sda /dev/sdb (and so on) are "\.\PhysicalDrive0" "\.\PhysicalDrive1" and so on (mind the backslashes, you need to escape them gaining ridiculous \\.\Physical...).

      Windows counterparts of partitions /dev/sda1 /dev/sda2 (and so on) are \.\C: \.\D: and so on (as long as partition is recognized by Windows).

      Note, that Windows (AFAIK) allows accesses only by multiples of sector size in length and only from sector boundary (which is 512 bytes for most HDDs - however for same new multi-terabyte disks it might be 4kbytes). For whole-device access starting from 0 you get this for free :)

      On Linux side you may access /dev/sd* just like plain files via QFile, so there's no need to use QProcess and indirectly dd - of course you need respective R / W rights to the device (just checked - works, Sample code follows). Probably the same applies to MacOS, but I don't know the respective device names. Probably on Windows you may access \.-ies the same way.

      -- Sample code --

      @#include <QtCore>
      #include <QFile>

      int main(int argc, char *argv[]) {

      #ifdef Q_WS_X11
      QFile hdd("/dev/sda");
      #endif

      #ifdef Q_WS_WIN
      QFile hdd("\\.\PhysicalDrive0");
      #endif

      #ifdef Q_WS_MACX
      QFile hdd("/dev/disk1"); // I'm not sure
      #endif

      if (!hdd.open(QIODevice::ReadOnly))
          return 255;
      
      QByteArray d = hdd.read(512);
      // Linux allows non-512 mutiplies
      
      hdd.close();
      
      QFile file&#40;"out.bin"&#41;;
      if (!file.open(QIODevice::WriteOnly))
          return 255;
      
      file.write(d);
      file.close();
      
      return 0;
      

      }@

      -- /Sample code --

      HTH.

      --
      Pawel Kraszewski
      http://kraszewscy.net / http://linuxpedia.pl

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Baca48
        wrote on 25 Jul 2012, 21:14 last edited by
        #3

        Thank you very much for the answer (or should I say 'dziękuję'?) :)

        Your sample code shows how to write an image from flash drive. I want to achieve something opposite - write file image to flash drive. Hovewer, I think it's gonna look the same or it's gonna be very similar.

        I suppose it's a lot easier with unix systems which recognise disks as normal files (to be more accurate, special type of files but still). I have no idea if it's possible to do that on Windows. I will try to use that solution and post the answer if it works or not!

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p.kraszewski
          wrote on 25 Jul 2012, 22:23 last edited by
          #4

          Yay! Nie ma sprawy! ("No problem")

          I just didn't want to trash my workstation's MBR and had no free USB drive laying around, so example is drive->file :)

          Well, just swap the paths around. Read file, write device. For Linux it's OK as long as you have permission to write to the device. This should be OK for the other two systems, too.

          --
          Pawel Kraszewski
          http://kraszewscy.net / http://linuxpedia.pl

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Baca48
            wrote on 30 Jul 2012, 10:34 last edited by
            #5

            This solution works perfect on Linux. Hovewer, there is a problem on Windows. I've successfully made image of pendrive and save it to file but I couldn't write anything to pendrive. It seems that the problem is with opening file with WriteOnly or ReadWrite flag:

            [code]
            QFile hdd("\\.\PhysicalDrive1");
            hdd.open(QIODevice::WriteOnly); // this line returns false
            [/code]

            I was using Windows Vista. Any ideas? Do I have to use WinAPI functions to get write access?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p.kraszewski
              wrote on 30 Jul 2012, 11:13 last edited by
              #6

              Try running the program with administrative credentials ("Run as administrator" or sth). As reading works, this may be simple ACL problem.

              --
              Pawel Kraszewski
              http://kraszewscy.net / http://linuxpedia.pl

              1 Reply Last reply
              0
              • B Baca48
                25 Jul 2012, 10:43

                Hi!

                I need to create a kind of usb flash drive creator. The problem is that it must be multiplatform and have GUI. GUI is not the problem because I'm quite familiar with Qt4. Hovewer, I don't know how to create flash drives from image file.

                On linux I could use dd for that (just invoke it with QProcess but still it's not the ideal solution) because it could make exact copy of binary file (image) to usb drive (e.g. /dev/sdc). How can I achieve that on my own using Qt4? I can't simple copy file by file because I want to create whole disk structure (partition, bootloader etc.). I hope I've explained well what I need.

                The solution must work on Windows and MacOS. Linux users can use simple bash script that uses dd to achieve the same thing.

                Thanks in advance for any help or tips.

                B Offline
                B Offline
                bulkflashdrive
                wrote on 23 Jul 2015, 11:27 last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved