1
0

main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /**
  2. * u3-tool - U3 USB stick manager
  3. * Copyright (C) 2009 Daviedev, daviedev@users.sourceforge.net
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #if HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include "u3.h"
  29. #include "u3_commands.h"
  30. #include "u3_scsi.h"
  31. #include "u3_error.h"
  32. #include "secure_input.h"
  33. #include "display_progress.h"
  34. #define TRUE 1
  35. #define FALSE 0
  36. #define MAX_SIZE_STRING_LENGTH 100
  37. #define MAX_FILENAME_STRING_LENGTH 1024
  38. #define MAX_PASSWORD_LENGTH 1024
  39. char *version = VERSION;
  40. int debug = 0;
  41. int batch_mode = 0;
  42. enum action_t { unknown, load, partition, dump, info, unlock, change_password,
  43. enable_security, disable_security };
  44. /********************************** Helpers ***********************************/
  45. /**
  46. * Ask confirmation of user.
  47. *
  48. * @returns TRUE if user wants to continue else FALSE to abort.
  49. */
  50. int confirm() {
  51. char c;
  52. int retval;
  53. int done;
  54. if (batch_mode != 0)
  55. return TRUE;
  56. retval = FALSE;
  57. done = FALSE;
  58. do {
  59. printf("\nAre you sure you want to continue? [yn] ");
  60. c = fgetc(stdin);
  61. if (c == 'y' || c == 'Y') {
  62. retval = TRUE;
  63. done = TRUE;
  64. } else if (c == 'n' || c == 'N') {
  65. retval = FALSE;
  66. done = TRUE;
  67. }
  68. // drop any extra chars and the new line
  69. while (c != '\n' && c != EOF) {
  70. c = fgetc(stdin);
  71. }
  72. } while (!done);
  73. return retval;
  74. }
  75. /**
  76. * Symbols of size multiplication factors.
  77. */
  78. char factor_symbols[] = "kMGTPE";
  79. /**
  80. * Print bytes is human readable fashion.
  81. *
  82. * @param size Data size to print
  83. */
  84. void print_human_size(size_t size) {
  85. float fsize = 0;
  86. unsigned int factor = 0;
  87. fsize = size;
  88. while (fsize > 1024) {
  89. fsize /= 1024;
  90. factor++;
  91. }
  92. printf("%.2f %cB", fsize, factor_symbols[factor-1]);
  93. }
  94. /**
  95. * Get pin tries left
  96. *
  97. * @param device U3 device handle
  98. * @param tries Used to return the number of tries left till device is blocked, or 0 if already blocked
  99. *
  100. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  101. * an error string can be obtained using u3_error()
  102. */
  103. int get_tries_left(u3_handle_t *device, int *tries) {
  104. struct dpart_info dpinfo;
  105. struct property_0C security_properties;
  106. *tries = 0;
  107. if (u3_data_partition_info(device, &dpinfo) != U3_SUCCESS) {
  108. fprintf(stderr, "u3_data_partition_info() failed: %s\n",
  109. u3_error_msg(device));
  110. return U3_FAILURE;
  111. }
  112. if (u3_read_device_property(device, 0x0C,
  113. (uint8_t *) &security_properties,
  114. sizeof(security_properties)
  115. ) != U3_SUCCESS)
  116. {
  117. fprintf(stderr, "u3_read_device_property() failed for "
  118. "property 0x0C: %s\n", u3_error_msg(device));
  119. return U3_FAILURE;
  120. }
  121. *tries = security_properties.max_pass_try - dpinfo.pass_try;
  122. return U3_SUCCESS;
  123. }
  124. /********************************** Actions ***********************************/
  125. int do_load(u3_handle_t *device, char *iso_filename) {
  126. struct stat file_stat;
  127. struct part_info pinfo;
  128. uint32_t cd_size;
  129. FILE *fp;
  130. uint8_t buffer[U3_BLOCK_SIZE];
  131. unsigned int bytes_read=0;
  132. unsigned int block_num=0;
  133. unsigned int block_cnt=0;
  134. // determine file size
  135. if (stat(iso_filename, &file_stat) == -1) {
  136. perror("Failed stating iso file");
  137. return EXIT_FAILURE;
  138. }
  139. if (file_stat.st_size == 0) {
  140. fprintf(stderr, "ISO file is empty\n");
  141. return EXIT_FAILURE;
  142. }
  143. cd_size = file_stat.st_size / U3_SECTOR_SIZE;
  144. if (file_stat.st_size % U3_SECTOR_SIZE)
  145. cd_size++;
  146. block_cnt = file_stat.st_size / U3_BLOCK_SIZE;
  147. if (file_stat.st_size % U3_BLOCK_SIZE)
  148. block_cnt++;
  149. // check partition size
  150. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  151. fprintf(stderr, "u3_partition_info() failed: %s\n",
  152. u3_error_msg(device));
  153. return EXIT_FAILURE;
  154. }
  155. if (cd_size > pinfo.cd_size) {
  156. fprintf(stderr, "CD image(%lu byte) is to big for current cd "
  157. "partition(%u byte), please repartition device.\n",
  158. file_stat.st_size,
  159. U3_SECTOR_SIZE * pinfo.cd_size);
  160. return EXIT_FAILURE;
  161. }
  162. // open file
  163. if ((fp = fopen(iso_filename, "rb")) == NULL) {
  164. perror("Failed opening iso file");
  165. return EXIT_FAILURE;
  166. }
  167. // write file to device
  168. block_num = 0;
  169. do {
  170. display_progress(block_num, block_cnt);
  171. bytes_read = fread(buffer, sizeof(uint8_t), U3_BLOCK_SIZE, fp);
  172. if (bytes_read != U3_BLOCK_SIZE) {
  173. if (ferror(fp)) {
  174. fclose(fp);
  175. perror("\nFailed reading iso file");
  176. return EXIT_FAILURE;
  177. } else if (bytes_read == 0) {
  178. continue;
  179. } else if (bytes_read > U3_BLOCK_SIZE) {
  180. fprintf(stderr, "\nRead more bytes then requested, impossible");
  181. return EXIT_FAILURE;
  182. } else {
  183. // zeroize rest of buffer to prevent writing garbage
  184. memset(buffer+bytes_read, 0, U3_BLOCK_SIZE-bytes_read);
  185. }
  186. }
  187. if (u3_cd_write(device, block_num, buffer) != U3_SUCCESS) {
  188. fclose(fp);
  189. fprintf(stderr, "\nu3_cd_write() failed: %s\n", u3_error_msg(device));
  190. return EXIT_FAILURE;
  191. }
  192. block_num++;
  193. } while (!feof(fp));
  194. display_progress(block_num, block_cnt);
  195. putchar('\n');
  196. fclose(fp);
  197. printf("OK\n");
  198. return EXIT_SUCCESS;
  199. }
  200. int do_partition(u3_handle_t *device, char *size_string) {
  201. uint64_t size;
  202. uint32_t cd_sectors;
  203. // determine file size
  204. size = strtoll(size_string, NULL, 0);
  205. cd_sectors = size / U3_SECTOR_SIZE;
  206. if (size % U3_SECTOR_SIZE)
  207. cd_sectors++;
  208. // repartition
  209. if (u3_partition(device, cd_sectors) != U3_SUCCESS) {
  210. fprintf(stderr, "u3_partition() failed: %s\n", u3_error_msg(device));
  211. return EXIT_FAILURE;
  212. }
  213. // reset device to make partitioning active
  214. if (u3_reset(device) != U3_SUCCESS) {
  215. fprintf(stderr, "u3_reset() failed: %s\n", u3_error_msg(device));
  216. return EXIT_FAILURE;
  217. }
  218. return EXIT_SUCCESS;
  219. }
  220. int do_unlock(u3_handle_t *device, char *password) {
  221. int result=0;
  222. int tries_left=0;
  223. // check password retry counter
  224. if (get_tries_left(device, &tries_left) != U3_SUCCESS) {
  225. return EXIT_FAILURE;
  226. }
  227. if (tries_left == 0) {
  228. printf("Unable to unlock, device is blocked\n");
  229. return EXIT_FAILURE;
  230. } else if (tries_left == 1) {
  231. printf("Warning: This is the your last password try. If this attempt fails,");
  232. printf(" all data on the data partition is lost.\n");
  233. if (!confirm())
  234. return EXIT_FAILURE;
  235. }
  236. // unlock device
  237. if (u3_unlock(device, password, &result) != U3_SUCCESS) {
  238. fprintf(stderr, "u3_unlock() failed: %s\n", u3_error_msg(device));
  239. return EXIT_FAILURE;
  240. }
  241. if (result) {
  242. printf("OK\n");
  243. return EXIT_SUCCESS;
  244. } else {
  245. printf("Password incorrect\n");
  246. return EXIT_FAILURE;
  247. }
  248. }
  249. int do_change_password(u3_handle_t *device, char *password,
  250. char *new_password)
  251. {
  252. int result=0;
  253. int tries_left=0;
  254. // check password retry counter
  255. if (get_tries_left(device, &tries_left) != U3_SUCCESS) {
  256. return EXIT_FAILURE;
  257. }
  258. if (tries_left == 0) {
  259. printf("Unable to change password, device is blocked\n");
  260. return EXIT_FAILURE;
  261. } else if (tries_left == 1) {
  262. printf("Warning: This is the your last password try. If this attempt fails,");
  263. printf(" all data on the data partition is lost.\n");
  264. if (!confirm())
  265. return EXIT_FAILURE;
  266. }
  267. // change password
  268. if (u3_change_password(device, password, new_password, &result)
  269. != U3_SUCCESS)
  270. {
  271. fprintf(stderr, "u3_change_password() failed: %s\n", u3_error_msg(device));
  272. return EXIT_FAILURE;
  273. }
  274. if (result) {
  275. printf("OK\n");
  276. return EXIT_SUCCESS;
  277. } else {
  278. printf("Password incorrect\n");
  279. return EXIT_FAILURE;
  280. }
  281. }
  282. int do_enable_security(u3_handle_t *device, char *password) {
  283. if (u3_enable_security(device, password) != U3_SUCCESS) {
  284. fprintf(stderr, "u3_enable_security() failed: %s\n",
  285. u3_error_msg(device));
  286. return EXIT_FAILURE;
  287. }
  288. return EXIT_SUCCESS;
  289. }
  290. int do_disable_security(u3_handle_t *device) {
  291. int result=0;
  292. // the enable security command is always possible without the correct
  293. // password, even if the device is locked. To work around asking for
  294. // a password we first call the enable security command with a known
  295. // password before calling disable security.
  296. if (u3_enable_security(device, "password") != U3_SUCCESS) {
  297. fprintf(stderr, "u3_enable_security() failed: %s\n",
  298. u3_error_msg(device));
  299. return EXIT_FAILURE;
  300. }
  301. if (u3_disable_security(device, "password", &result) != U3_SUCCESS) {
  302. fprintf(stderr, "u3_disable_security() failed: %s\n",
  303. u3_error_msg(device));
  304. return EXIT_FAILURE;
  305. }
  306. if (result) {
  307. printf("OK\n");
  308. return EXIT_SUCCESS;
  309. } else {
  310. printf("Password incorrect\n");
  311. return EXIT_FAILURE;
  312. }
  313. }
  314. int do_info(u3_handle_t *device) {
  315. struct part_info pinfo;
  316. struct dpart_info dpinfo;
  317. struct chip_info cinfo;
  318. struct property_03 device_properties;
  319. struct property_0C security_properties;
  320. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  321. fprintf(stderr, "u3_partition_info() failed: %s\n",
  322. u3_error_msg(device));
  323. return EXIT_FAILURE;
  324. }
  325. if (u3_data_partition_info(device, &dpinfo) != U3_SUCCESS) {
  326. fprintf(stderr, "u3_data_partition_info() failed: %s\n",
  327. u3_error_msg(device));
  328. return EXIT_FAILURE;
  329. }
  330. if (u3_chip_info(device, &cinfo) != U3_SUCCESS) {
  331. fprintf(stderr, "u3_chip_info() failed: %s\n",
  332. u3_error_msg(device));
  333. return EXIT_FAILURE;
  334. }
  335. if (u3_read_device_property(device, 0x03,
  336. (uint8_t *) &device_properties,
  337. sizeof(device_properties)
  338. ) != U3_SUCCESS)
  339. {
  340. fprintf(stderr, "u3_read_device_property() failed for "
  341. "property 0x03: %s\n", u3_error_msg(device));
  342. return EXIT_FAILURE;
  343. }
  344. if (u3_read_device_property(device, 0x0C,
  345. (uint8_t *) &security_properties,
  346. sizeof(security_properties)
  347. ) != U3_SUCCESS)
  348. {
  349. fprintf(stderr, "u3_read_device_property() failed for "
  350. "property 0x0C: %s\n", u3_error_msg(device));
  351. return EXIT_FAILURE;
  352. }
  353. printf("Total device size: ");
  354. print_human_size(1ll * U3_SECTOR_SIZE * device_properties.device_size);
  355. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * device_properties.device_size);
  356. printf("CD size: ");
  357. print_human_size(1ll * U3_SECTOR_SIZE * pinfo.cd_size);
  358. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * pinfo.cd_size);
  359. if (dpinfo.secured_size == 0) {
  360. printf("Data partition size: ");
  361. print_human_size(1ll * U3_SECTOR_SIZE * dpinfo.total_size);
  362. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * dpinfo.total_size);
  363. } else {
  364. printf("Secured zone size: ");
  365. print_human_size(1ll * U3_SECTOR_SIZE * dpinfo.secured_size);
  366. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * dpinfo.secured_size);
  367. printf("Secure zone status: ");
  368. if(dpinfo.unlocked) {
  369. printf("unlocked\n");
  370. } else {
  371. if (security_properties.max_pass_try == dpinfo.pass_try) {
  372. printf("BLOCKED!\n");
  373. } else {
  374. printf("locked (%u tries left)\n", security_properties.max_pass_try - dpinfo.pass_try);
  375. }
  376. }
  377. }
  378. return EXIT_SUCCESS;
  379. }
  380. int do_dump(u3_handle_t *device) {
  381. int retval = EXIT_SUCCESS;
  382. struct part_info pinfo;
  383. struct dpart_info dpinfo;
  384. struct chip_info cinfo;
  385. struct property_03 device_properties;
  386. struct property_0C security_properties;
  387. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  388. fprintf(stderr, "u3_partition_info() failed: %s\n",
  389. u3_error_msg(device));
  390. retval = EXIT_FAILURE;
  391. } else {
  392. printf("Partition info:\n");
  393. printf(" - Partition count: 0x%.2x\n", pinfo.partition_count);
  394. printf(" - Data partition size: %llu byte(0x%.8x)\n",
  395. 1ll * U3_SECTOR_SIZE * pinfo.data_size, pinfo.data_size);
  396. printf(" - Unknown1: 0x%.8x\n", pinfo.unknown1);
  397. printf(" - CD size: %llu byte(0x%.8x)\n", 1ll * U3_SECTOR_SIZE * pinfo.cd_size,
  398. pinfo.cd_size);
  399. printf(" - Unknown2: 0x%.8x\n", pinfo.unknown2);
  400. printf("\n");
  401. }
  402. if (u3_data_partition_info(device, &dpinfo) != U3_SUCCESS) {
  403. fprintf(stderr, "u3_data_partition_info() failed: %s\n",
  404. u3_error_msg(device));
  405. retval = EXIT_FAILURE;
  406. } else {
  407. printf("Data partition info:\n");
  408. printf(" - Data partition size: %llu byte(0x%.8x)\n",
  409. 1ll * U3_SECTOR_SIZE * dpinfo.total_size , dpinfo.total_size);
  410. printf(" - Secured zone size: %llu byte(0x%.8x)\n",
  411. 1ll * U3_SECTOR_SIZE * dpinfo.secured_size, dpinfo.secured_size);
  412. printf(" - Unlocked: 0x%.8x\n", dpinfo.unlocked);
  413. printf(" - Password try: 0x%.8x\n", dpinfo.pass_try);
  414. printf("\n");
  415. }
  416. if (u3_chip_info(device, &cinfo) != U3_SUCCESS) {
  417. fprintf(stderr, "u3_chip_info() failed: %s\n",
  418. u3_error_msg(device));
  419. retval = EXIT_FAILURE;
  420. } else {
  421. printf("Chip info:\n");
  422. printf(" - Manufacturer: %.*s\n", U3_MAX_CHIP_MANUFACTURER_LEN,
  423. cinfo.manufacturer);
  424. printf(" - Revision: %.*s\n", U3_MAX_CHIP_REVISION_LEN,
  425. cinfo.revision);
  426. printf("\n");
  427. }
  428. if (u3_read_device_property(device, 0x03,
  429. (uint8_t *) &device_properties,
  430. sizeof(device_properties)
  431. ) != U3_SUCCESS)
  432. {
  433. fprintf(stderr, "u3_read_device_property() failed for "
  434. "property 0x03: %s\n", u3_error_msg(device));
  435. retval = EXIT_FAILURE;
  436. } else {
  437. if (device_properties.hdr.length !=
  438. sizeof(device_properties))
  439. {
  440. fprintf(stderr, "Length of property 0x03 is not the "
  441. "expected length. (len=%u)\n",
  442. device_properties.hdr.length);
  443. retval = EXIT_FAILURE;
  444. } else {
  445. printf("Property page 0x03:\n");
  446. printf(" - Device size: %llu byte(0x%.8x)\n",
  447. 1ll * U3_SECTOR_SIZE * device_properties.device_size, device_properties.device_size);
  448. printf(" - Device serial: %.*s\n",U3_MAX_SERIAL_LEN,
  449. device_properties.serial);
  450. printf(" - Full record length: 0x%.8x\n",
  451. device_properties.full_length);
  452. printf(" - Unknown1: 0x%.2x\n",
  453. device_properties.unknown1);
  454. printf(" - Unknown2: 0x%.8x\n",
  455. device_properties.unknown2);
  456. printf(" - Unknown3: 0x%.8x\n",
  457. device_properties.unknown3);
  458. printf("\n");
  459. }
  460. }
  461. if (u3_read_device_property(device, 0x0C,
  462. (uint8_t *) &security_properties,
  463. sizeof(security_properties)
  464. ) != U3_SUCCESS)
  465. {
  466. fprintf(stderr, "u3_read_device_property() failed for "
  467. "property 0x0C: %s\n", u3_error_msg(device));
  468. retval = EXIT_FAILURE;
  469. } else {
  470. if (security_properties.hdr.length !=
  471. sizeof(security_properties))
  472. {
  473. fprintf(stderr, "Length of property 0x0C is not the "
  474. "expected length. (len=%u)\n",
  475. security_properties.hdr.length);
  476. retval = EXIT_FAILURE;
  477. } else {
  478. printf("Property page 0x0C:\n");
  479. printf(" - Max. pass. try: %u",
  480. security_properties.max_pass_try);
  481. printf("\n");
  482. }
  483. }
  484. return EXIT_SUCCESS;
  485. }
  486. /************************************ Main ************************************/
  487. void usage(const char *name) {
  488. printf("u3-tool %s - U3 USB stick manager\n", version);
  489. printf("\n");
  490. printf("Usage: %s [options] <device name>\n", name);
  491. printf("\n");
  492. printf("Options:\n");
  493. printf("\t-c Change password\n");
  494. printf("\t-d Disable device security\n");
  495. printf("\t-D Dump all raw info(for debug)\n");
  496. printf("\t-e Enable device security\n");
  497. printf("\t-h Print this help message\n");
  498. printf("\t-i Display device info\n");
  499. printf("\t-l <cd image> Load CD image into device\n");
  500. printf("\t-p <cd size> Repartition device\n");
  501. printf("\t-u Unlock device\n");
  502. printf("\t-v Use verbose output\n");
  503. printf("\t-V Print version information\n");
  504. printf("\n");
  505. printf("The device name depends on the used subsystem.\n");
  506. printf("Examples: '/dev/sda0'(sg), 'scan'(USB),'vid:pid'(USB), "
  507. "'e'(Windows)\n");
  508. }
  509. void print_version() {
  510. printf("u3-tool %s\n", version);
  511. printf("\n");
  512. printf("Copyright (C) 2009\n");
  513. printf("This is free software; see the source for copying "
  514. "conditions. There is NO\nwarranty; not even for "
  515. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
  516. }
  517. int main(int argc, char *argv[]) {
  518. u3_handle_t device;
  519. int c;
  520. enum action_t action = unknown;
  521. char *device_name;
  522. char filename_string[MAX_FILENAME_STRING_LENGTH+1];
  523. char size_string[MAX_SIZE_STRING_LENGTH+1];
  524. int ask_password = TRUE;
  525. char password[MAX_PASSWORD_LENGTH+1];
  526. int ask_new_password = TRUE;
  527. char new_password[MAX_PASSWORD_LENGTH+1];
  528. int retval = EXIT_SUCCESS;
  529. //
  530. // parse options
  531. //
  532. while ((c = getopt(argc, argv, "cdDehil:p:uvVz")) != -1) {
  533. switch (c) {
  534. case 'c':
  535. action = change_password;
  536. break;
  537. case 'd':
  538. action = disable_security;
  539. break;
  540. case 'e':
  541. action = enable_security;
  542. break;
  543. case 'i':
  544. action = info;
  545. break;
  546. case 'l':
  547. action = load;
  548. strncpy(filename_string, optarg, MAX_FILENAME_STRING_LENGTH);
  549. filename_string[MAX_FILENAME_STRING_LENGTH] = '\0';
  550. break;
  551. case 'p':
  552. action = partition;
  553. strncpy(size_string, optarg, MAX_SIZE_STRING_LENGTH);
  554. size_string[MAX_SIZE_STRING_LENGTH] = '\0';
  555. break;
  556. case 'u':
  557. action = unlock;
  558. break;
  559. case 'v':
  560. debug = 1;
  561. break;
  562. case 'V':
  563. print_version();
  564. exit(EXIT_SUCCESS);
  565. break;
  566. case 'D':
  567. action = dump;
  568. break;
  569. case 'h':
  570. default:
  571. usage(argv[0]);
  572. exit(EXIT_FAILURE);
  573. break;
  574. }
  575. }
  576. //
  577. // parse arguments
  578. //
  579. if (argc-optind < 1) {
  580. fprintf(stderr, "Not enough arguments\n");
  581. usage(argv[0]);
  582. exit(EXIT_FAILURE);
  583. }
  584. device_name = argv[optind];
  585. if ((action == unlock || action == change_password)
  586. && ask_password)
  587. {
  588. int proceed = 0;
  589. do {
  590. fprintf(stderr, "Enter password: ");
  591. secure_input(password, sizeof(password));
  592. if (strlen(password) == 0) {
  593. fprintf(stderr, "Password Empty\n");
  594. } else {
  595. proceed = 1;
  596. }
  597. } while (!proceed);
  598. }
  599. if ((action == change_password || action == enable_security)
  600. && ask_new_password)
  601. {
  602. int proceed = 0;
  603. char validate_password[MAX_PASSWORD_LENGTH+1];
  604. do {
  605. fprintf(stderr, "Enter new password: ");
  606. secure_input(new_password, sizeof(new_password));
  607. if (strlen(new_password) == 0) {
  608. fprintf(stderr, "Password Empty\n");
  609. continue;
  610. }
  611. fprintf(stderr, "Reenter new password: ");
  612. secure_input(validate_password, sizeof(validate_password));
  613. if (strcmp(new_password, validate_password) == 0) {
  614. proceed = 1;
  615. } else {
  616. fprintf(stderr, "Passwords don't match\n");
  617. }
  618. } while (!proceed);
  619. }
  620. //
  621. // open the device
  622. //
  623. if (u3_open(&device, device_name)) {
  624. fprintf(stderr, "Error opening device: %s\n", u3_error_msg(&device));
  625. exit(EXIT_FAILURE);
  626. }
  627. //
  628. // preform action
  629. //
  630. switch (action) {
  631. case load:
  632. retval = do_load(&device, filename_string);
  633. break;
  634. case partition:
  635. printf("\n");
  636. printf("WARNING: Loading a new cd image causes the ");
  637. printf("whole device to be whiped. This INCLUDES\n ");
  638. printf("the data partition.\n");
  639. printf("I repeat: ANY EXCISTING DATA WILL BE LOST!\n");
  640. if (confirm())
  641. retval = do_partition(&device, size_string);
  642. break;
  643. case dump:
  644. retval = do_dump(&device);
  645. break;
  646. case info:
  647. retval = do_info(&device);
  648. break;
  649. case unlock:
  650. retval = do_unlock(&device, password);
  651. break;
  652. case change_password:
  653. retval = do_change_password(&device, password, new_password);
  654. break;
  655. case enable_security:
  656. printf("WARNING: This will delete all data on the data ");
  657. printf("partition\n");
  658. if (confirm())
  659. retval = do_enable_security(&device, new_password);
  660. break;
  661. case disable_security:
  662. printf("WARNING: This will delete all data on the data ");
  663. printf("partition\n");
  664. if (confirm())
  665. retval = do_disable_security(&device);
  666. break;
  667. default:
  668. fprintf(stderr, "No action specified, use '-h' option for help.\n");
  669. break;
  670. }
  671. //
  672. // clean up
  673. //
  674. u3_close(&device);
  675. return retval;
  676. }