main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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, reset_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(uint64_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, char *password) {
  291. int result=0;
  292. int tries_left=0;
  293. // check password retry counter
  294. if (get_tries_left(device, &tries_left) != U3_SUCCESS) {
  295. return EXIT_FAILURE;
  296. }
  297. if (tries_left == 0) {
  298. printf("Unable to disable security, device is blocked\n");
  299. return EXIT_FAILURE;
  300. } else if (tries_left == 1) {
  301. printf("Warning: This is the your last password try. If this attempt fails,");
  302. printf(" all data on the data partition is lost.\n");
  303. if (!confirm())
  304. return EXIT_FAILURE;
  305. }
  306. // disable security
  307. if (u3_disable_security(device, password, &result) != U3_SUCCESS) {
  308. fprintf(stderr, "u3_disable_security() failed: %s\n",
  309. u3_error_msg(device));
  310. return EXIT_FAILURE;
  311. }
  312. if (result) {
  313. printf("OK\n");
  314. return EXIT_SUCCESS;
  315. } else {
  316. printf("Password incorrect\n");
  317. return EXIT_FAILURE;
  318. }
  319. }
  320. int do_reset_security(u3_handle_t *device) {
  321. int result=0;
  322. // the enable security command is always possible without the correct
  323. // password, even if the device is locked. To work around asking for
  324. // a password we first call the enable security command with a known
  325. // password before calling disable security.
  326. if (u3_enable_security(device, "password") != U3_SUCCESS) {
  327. fprintf(stderr, "u3_enable_security() failed: %s\n",
  328. u3_error_msg(device));
  329. return EXIT_FAILURE;
  330. }
  331. if (u3_disable_security(device, "password", &result) != U3_SUCCESS) {
  332. fprintf(stderr, "u3_disable_security() failed: %s\n",
  333. u3_error_msg(device));
  334. return EXIT_FAILURE;
  335. }
  336. if (result) {
  337. printf("OK\n");
  338. return EXIT_SUCCESS;
  339. } else {
  340. printf("Failed\n");
  341. return EXIT_FAILURE;
  342. }
  343. }
  344. int do_info(u3_handle_t *device) {
  345. struct part_info pinfo;
  346. struct dpart_info dpinfo;
  347. struct chip_info cinfo;
  348. struct property_03 device_properties;
  349. struct property_0C security_properties;
  350. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  351. fprintf(stderr, "u3_partition_info() failed: %s\n",
  352. u3_error_msg(device));
  353. return EXIT_FAILURE;
  354. }
  355. if (u3_data_partition_info(device, &dpinfo) != U3_SUCCESS) {
  356. fprintf(stderr, "u3_data_partition_info() failed: %s\n",
  357. u3_error_msg(device));
  358. return EXIT_FAILURE;
  359. }
  360. if (u3_chip_info(device, &cinfo) != U3_SUCCESS) {
  361. fprintf(stderr, "u3_chip_info() failed: %s\n",
  362. u3_error_msg(device));
  363. return EXIT_FAILURE;
  364. }
  365. if (u3_read_device_property(device, 0x03,
  366. (uint8_t *) &device_properties,
  367. sizeof(device_properties)
  368. ) != U3_SUCCESS)
  369. {
  370. fprintf(stderr, "u3_read_device_property() failed for "
  371. "property 0x03: %s\n", u3_error_msg(device));
  372. return EXIT_FAILURE;
  373. }
  374. if (u3_read_device_property(device, 0x0C,
  375. (uint8_t *) &security_properties,
  376. sizeof(security_properties)
  377. ) != U3_SUCCESS)
  378. {
  379. fprintf(stderr, "u3_read_device_property() failed for "
  380. "property 0x0C: %s\n", u3_error_msg(device));
  381. return EXIT_FAILURE;
  382. }
  383. printf("Total device size: ");
  384. print_human_size(1ll * U3_SECTOR_SIZE * device_properties.device_size);
  385. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * device_properties.device_size);
  386. printf("CD size: ");
  387. print_human_size(1ll * U3_SECTOR_SIZE * pinfo.cd_size);
  388. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * pinfo.cd_size);
  389. if (dpinfo.secured_size == 0) {
  390. printf("Data partition size: ");
  391. print_human_size(1ll * U3_SECTOR_SIZE * dpinfo.total_size);
  392. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * dpinfo.total_size);
  393. } else {
  394. printf("Secured zone size: ");
  395. print_human_size(1ll * U3_SECTOR_SIZE * dpinfo.secured_size);
  396. printf(" (%llu bytes)\n", 1ll * U3_SECTOR_SIZE * dpinfo.secured_size);
  397. printf("Secure zone status: ");
  398. if(dpinfo.unlocked) {
  399. printf("unlocked\n");
  400. } else {
  401. if (security_properties.max_pass_try == dpinfo.pass_try) {
  402. printf("BLOCKED!\n");
  403. } else {
  404. printf("locked (%u tries left)\n", security_properties.max_pass_try - dpinfo.pass_try);
  405. }
  406. }
  407. }
  408. return EXIT_SUCCESS;
  409. }
  410. int do_dump(u3_handle_t *device) {
  411. int retval = EXIT_SUCCESS;
  412. struct part_info pinfo;
  413. struct dpart_info dpinfo;
  414. struct chip_info cinfo;
  415. struct property_03 device_properties;
  416. struct property_0C security_properties;
  417. if (u3_partition_info(device, &pinfo) != U3_SUCCESS) {
  418. fprintf(stderr, "u3_partition_info() failed: %s\n",
  419. u3_error_msg(device));
  420. retval = EXIT_FAILURE;
  421. } else {
  422. printf("Partition info:\n");
  423. printf(" - Partition count: 0x%.2x\n", pinfo.partition_count);
  424. printf(" - Data partition size: %llu byte(0x%.8x)\n",
  425. 1ll * U3_SECTOR_SIZE * pinfo.data_size, pinfo.data_size);
  426. printf(" - Unknown1: 0x%.8x\n", pinfo.unknown1);
  427. printf(" - CD size: %llu byte(0x%.8x)\n", 1ll * U3_SECTOR_SIZE * pinfo.cd_size,
  428. pinfo.cd_size);
  429. printf(" - Unknown2: 0x%.8x\n", pinfo.unknown2);
  430. printf("\n");
  431. }
  432. if (u3_data_partition_info(device, &dpinfo) != U3_SUCCESS) {
  433. fprintf(stderr, "u3_data_partition_info() failed: %s\n",
  434. u3_error_msg(device));
  435. retval = EXIT_FAILURE;
  436. } else {
  437. printf("Data partition info:\n");
  438. printf(" - Data partition size: %llu byte(0x%.8x)\n",
  439. 1ll * U3_SECTOR_SIZE * dpinfo.total_size , dpinfo.total_size);
  440. printf(" - Secured zone size: %llu byte(0x%.8x)\n",
  441. 1ll * U3_SECTOR_SIZE * dpinfo.secured_size, dpinfo.secured_size);
  442. printf(" - Unlocked: 0x%.8x\n", dpinfo.unlocked);
  443. printf(" - Password try: 0x%.8x\n", dpinfo.pass_try);
  444. printf("\n");
  445. }
  446. if (u3_chip_info(device, &cinfo) != U3_SUCCESS) {
  447. fprintf(stderr, "u3_chip_info() failed: %s\n",
  448. u3_error_msg(device));
  449. retval = EXIT_FAILURE;
  450. } else {
  451. printf("Chip info:\n");
  452. printf(" - Manufacturer: %.*s\n", U3_MAX_CHIP_MANUFACTURER_LEN,
  453. cinfo.manufacturer);
  454. printf(" - Revision: %.*s\n", U3_MAX_CHIP_REVISION_LEN,
  455. cinfo.revision);
  456. printf("\n");
  457. }
  458. if (u3_read_device_property(device, 0x03,
  459. (uint8_t *) &device_properties,
  460. sizeof(device_properties)
  461. ) != U3_SUCCESS)
  462. {
  463. fprintf(stderr, "u3_read_device_property() failed for "
  464. "property 0x03: %s\n", u3_error_msg(device));
  465. retval = EXIT_FAILURE;
  466. } else {
  467. if (device_properties.hdr.length !=
  468. sizeof(device_properties))
  469. {
  470. fprintf(stderr, "Length of property 0x03 is not the "
  471. "expected length. (len=%u)\n",
  472. device_properties.hdr.length);
  473. retval = EXIT_FAILURE;
  474. } else {
  475. printf("Property page 0x03:\n");
  476. printf(" - Device size: %llu byte(0x%.8x)\n",
  477. 1ll * U3_SECTOR_SIZE * device_properties.device_size, device_properties.device_size);
  478. printf(" - Device serial: %.*s\n",U3_MAX_SERIAL_LEN,
  479. device_properties.serial);
  480. printf(" - Full record length: 0x%.8x\n",
  481. device_properties.full_length);
  482. printf(" - Unknown1: 0x%.2x\n",
  483. device_properties.unknown1);
  484. printf(" - Unknown2: 0x%.8x\n",
  485. device_properties.unknown2);
  486. printf(" - Unknown3: 0x%.8x\n",
  487. device_properties.unknown3);
  488. printf("\n");
  489. }
  490. }
  491. if (u3_read_device_property(device, 0x0C,
  492. (uint8_t *) &security_properties,
  493. sizeof(security_properties)
  494. ) != U3_SUCCESS)
  495. {
  496. fprintf(stderr, "u3_read_device_property() failed for "
  497. "property 0x0C: %s\n", u3_error_msg(device));
  498. retval = EXIT_FAILURE;
  499. } else {
  500. if (security_properties.hdr.length !=
  501. sizeof(security_properties))
  502. {
  503. fprintf(stderr, "Length of property 0x0C is not the "
  504. "expected length. (len=%u)\n",
  505. security_properties.hdr.length);
  506. retval = EXIT_FAILURE;
  507. } else {
  508. printf("Property page 0x0C:\n");
  509. printf(" - Max. pass. try: %u",
  510. security_properties.max_pass_try);
  511. printf("\n");
  512. }
  513. }
  514. return EXIT_SUCCESS;
  515. }
  516. /************************************ Main ************************************/
  517. void usage(const char *name) {
  518. printf("u3-tool %s - U3 USB stick manager\n", version);
  519. printf("\n");
  520. printf("Usage: %s [options] <device name>\n", name);
  521. printf("\n");
  522. printf("Options:\n");
  523. printf("\t-c Change password\n");
  524. printf("\t-d Disable device security\n");
  525. printf("\t-D Dump all raw info(for debug)\n");
  526. printf("\t-e Enable device security\n");
  527. printf("\t-h Print this help message\n");
  528. printf("\t-i Display device info\n");
  529. printf("\t-l <cd image> Load CD image into device\n");
  530. printf("\t-p <cd size> Repartition device\n");
  531. printf("\t-R Reset device security, destroying private data\n");
  532. printf("\t-u Unlock device\n");
  533. printf("\t-v Use verbose output\n");
  534. printf("\t-V Print version information\n");
  535. printf("\n");
  536. printf("For the device name use:\n %s\n", u3_subsystem_help);
  537. }
  538. void print_version() {
  539. printf("u3-tool %s\n", version);
  540. printf("subsystem: %s\n", u3_subsystem_name);
  541. printf("\n");
  542. printf("Copyright (C) 2009\n");
  543. printf("This is free software; see the source for copying "
  544. "conditions. There is NO\nwarranty; not even for "
  545. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
  546. }
  547. int main(int argc, char *argv[]) {
  548. u3_handle_t device;
  549. int c;
  550. enum action_t action = unknown;
  551. char *device_name;
  552. char filename_string[MAX_FILENAME_STRING_LENGTH+1];
  553. char size_string[MAX_SIZE_STRING_LENGTH+1];
  554. int ask_password = TRUE;
  555. char password[MAX_PASSWORD_LENGTH+1];
  556. int ask_new_password = TRUE;
  557. char new_password[MAX_PASSWORD_LENGTH+1];
  558. int retval = EXIT_SUCCESS;
  559. //
  560. // parse options
  561. //
  562. while ((c = getopt(argc, argv, "cdDehil:p:RuvVz")) != -1) {
  563. switch (c) {
  564. case 'c':
  565. action = change_password;
  566. break;
  567. case 'd':
  568. action = disable_security;
  569. break;
  570. case 'e':
  571. action = enable_security;
  572. break;
  573. case 'i':
  574. action = info;
  575. break;
  576. case 'l':
  577. action = load;
  578. strncpy(filename_string, optarg, MAX_FILENAME_STRING_LENGTH);
  579. filename_string[MAX_FILENAME_STRING_LENGTH] = '\0';
  580. break;
  581. case 'p':
  582. action = partition;
  583. strncpy(size_string, optarg, MAX_SIZE_STRING_LENGTH);
  584. size_string[MAX_SIZE_STRING_LENGTH] = '\0';
  585. break;
  586. case 'R':
  587. action = reset_security;
  588. break;
  589. case 'u':
  590. action = unlock;
  591. break;
  592. case 'v':
  593. debug = 1;
  594. break;
  595. case 'V':
  596. print_version();
  597. exit(EXIT_SUCCESS);
  598. break;
  599. case 'D':
  600. action = dump;
  601. break;
  602. case 'h':
  603. default:
  604. usage(argv[0]);
  605. exit(EXIT_FAILURE);
  606. break;
  607. }
  608. }
  609. //
  610. // parse arguments
  611. //
  612. if (argc-optind < 1) {
  613. fprintf(stderr, "Not enough arguments\n");
  614. usage(argv[0]);
  615. exit(EXIT_FAILURE);
  616. }
  617. device_name = argv[optind];
  618. //
  619. // open the device
  620. //
  621. if (u3_open(&device, device_name)) {
  622. fprintf(stderr, "Error opening device: %s\n", u3_error_msg(&device));
  623. exit(EXIT_FAILURE);
  624. }
  625. //
  626. // ask passwords
  627. //
  628. if ((action == unlock || action == change_password || action == disable_security)
  629. && ask_password)
  630. {
  631. int proceed = 0;
  632. do {
  633. fprintf(stderr, "Enter password: ");
  634. secure_input(password, sizeof(password));
  635. if (strlen(password) == 0) {
  636. fprintf(stderr, "Password Empty\n");
  637. } else {
  638. proceed = 1;
  639. }
  640. } while (!proceed);
  641. }
  642. if ((action == change_password || action == enable_security)
  643. && ask_new_password)
  644. {
  645. int proceed = 0;
  646. char validate_password[MAX_PASSWORD_LENGTH+1];
  647. do {
  648. fprintf(stderr, "Enter new password: ");
  649. secure_input(new_password, sizeof(new_password));
  650. if (strlen(new_password) == 0) {
  651. fprintf(stderr, "Password Empty\n");
  652. continue;
  653. }
  654. fprintf(stderr, "Reenter new password: ");
  655. secure_input(validate_password, sizeof(validate_password));
  656. if (strcmp(new_password, validate_password) == 0) {
  657. proceed = 1;
  658. } else {
  659. fprintf(stderr, "Passwords don't match\n");
  660. }
  661. memset(validate_password, 0, sizeof(validate_password));
  662. } while (!proceed);
  663. }
  664. //
  665. // preform action
  666. //
  667. switch (action) {
  668. case load:
  669. retval = do_load(&device, filename_string);
  670. break;
  671. case partition:
  672. printf("\n");
  673. printf("WARNING: Loading a new cd image causes the ");
  674. printf("whole device to be whiped. This INCLUDES\n ");
  675. printf("the data partition.\n");
  676. printf("I repeat: ANY EXCISTING DATA WILL BE LOST!\n");
  677. if (confirm())
  678. retval = do_partition(&device, size_string);
  679. break;
  680. case dump:
  681. retval = do_dump(&device);
  682. break;
  683. case info:
  684. retval = do_info(&device);
  685. break;
  686. case unlock:
  687. retval = do_unlock(&device, password);
  688. break;
  689. case change_password:
  690. retval = do_change_password(&device, password, new_password);
  691. break;
  692. case enable_security:
  693. printf("WARNING: This will delete all data on the data ");
  694. printf("partition\n");
  695. if (confirm())
  696. retval = do_enable_security(&device, new_password);
  697. break;
  698. case disable_security:
  699. retval = do_disable_security(&device, password);
  700. break;
  701. case reset_security:
  702. printf("WARNING: This will delete all data on the data ");
  703. printf("partition\n");
  704. if (confirm())
  705. retval = do_reset_security(&device);
  706. break;
  707. default:
  708. fprintf(stderr, "No action specified, use '-h' option for help.\n");
  709. break;
  710. }
  711. //
  712. // clean up
  713. //
  714. memset(password, 0, sizeof(password));
  715. memset(new_password, 0, sizeof(new_password));
  716. u3_close(&device);
  717. return retval;
  718. }