Fix input of size fieds and CDROM sector length in scsi2sd-util
[SCSI2SD.git] / software / scsi2sd-util / scsi2sd-bulk.cc
1 #include "SCSI2SD_Bootloader.hh"
2 #include "SCSI2SD_HID.hh"
3 #include "Firmware.hh"
4
5 #include <iostream>
6 #include <algorithm>
7 #include <iomanip>
8 #include <vector>
9 #include <set>
10 #include <sstream>
11
12 #if __cplusplus >= 201103L
13 #include <cstdint>
14 #include <memory>
15 using std::shared_ptr;
16 #else
17 #include <stdint.h>
18 #include <tr1/memory>
19 using std::tr1::shared_ptr;
20 #endif
21
22 #include <unistd.h>
23
24
25 #define MIN_FIRMWARE_VERSION 0x0400
26
27 using namespace SCSI2SD;
28
29 extern "C"
30 void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
31 {
32         std::cout << "." << std::flush;
33 }
34
35 int main(int argc, char** argv)
36 {
37         if (argc < 2)
38         {
39                 std::cerr << "Usage: " << argv[0] << " {firmware.cyacd}" << std::endl;
40                 exit(1);
41         }
42         std::string filename(argv[1]);
43
44         std::cout << "Searching for SCSI2SD device ..." << std::endl;
45
46         while (true)
47         {
48                 shared_ptr<Bootloader> bootloader;
49                 try
50                 {
51                         shared_ptr<HID> hid(HID::Open());
52                         if (hid)
53                         {
54                                 std::cout << "Resetting SCSI2SD into bootloader" << std::endl;
55                                 usleep(100000);
56                                 hid->enterBootloader();
57                                 usleep(100000);
58                         }
59
60                         bootloader.reset(Bootloader::Open());
61                 } catch (...) {}
62
63                 if (bootloader)
64                 {
65                         std::cout << "Upgrading firmware from file: " << filename << std::endl;
66
67                         try
68                         {
69                                 bootloader->load(filename, &ProgressUpdate);
70                                 usleep(2000000);
71                                 std::cerr << "\nFirmware update successful" << std::endl;
72
73
74                                 // Wait for disconnect
75                                 shared_ptr<HID> hid(HID::Open());
76
77                                 while (bootloader || hid)
78                                 {
79                                         usleep(100000);
80                                         try {
81                                                 hid.reset(HID::Open());
82                                                 usleep(100000);
83                                                 bootloader.reset(Bootloader::Open());
84                                                 usleep(100000);
85                                         } catch (...) {}
86                                 }
87                                 std::cout << "Searching for SCSI2SD device ..." << std::endl;
88                         }
89                         catch (std::exception& e)
90                         {
91                                 std::cerr << "Firmware update failed: " << e.what() << std::endl;
92                                 abort();
93                         }
94
95                 }
96                 else
97                 {
98                         usleep(100000);
99                 }
100         }
101 }