1
0

u3_commands.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * u3-tool - U3 USB stick manager
  3. * Copyright (C) 2007 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. #ifndef __U3_COMMAND__
  20. #define __U3_COMMAND__
  21. /**
  22. * @file u3_commands.h
  23. *
  24. * This file declares the functions for controlling the
  25. * U3 device.
  26. */
  27. #include "u3.h"
  28. /********************************* structures *********************************/
  29. /**
  30. * Device property header structure
  31. */
  32. struct property_header {
  33. uint16_t id; /* Property ID */
  34. uint16_t unknown1;
  35. uint16_t length; /* Length of property including header */
  36. } __attribute__ ((packed));
  37. /**
  38. * Device property 0x03 structure
  39. */
  40. #define U3_MAX_SERIAL_LEN 16
  41. struct property_03 {
  42. struct property_header hdr; /* property header */
  43. uint32_t full_length; /* Actual Full length of property???? */
  44. uint8_t unknown1;
  45. uint32_t unknown2;
  46. char serial[U3_MAX_SERIAL_LEN]; /* Device Serial number */
  47. uint32_t unknown3;
  48. uint32_t device_size; /* Device size in sectors */
  49. } __attribute__ ((packed));
  50. /**
  51. * Device property 0x0C structure
  52. */
  53. struct property_0C {
  54. struct property_header hdr; /* property header */
  55. uint32_t max_pass_try; /* Maximum wrong password try */
  56. } __attribute__ ((packed));
  57. /**
  58. * partition information structure
  59. */
  60. struct part_info {
  61. uint8_t partition_count;
  62. uint32_t unknown1;
  63. uint32_t data_size; // in sectors
  64. uint32_t unknown2;
  65. uint32_t cd_size; // in sectors
  66. } __attribute__ ((packed));
  67. /**
  68. * Data partition information structure
  69. */
  70. struct dpart_info {
  71. uint32_t total_size; // size of data partition in sectors
  72. uint32_t secured_size; // size of secured zone in sectors
  73. uint32_t unlocked; // 1 = unlocked, 0 = locked
  74. uint32_t pass_try; // number of times incorrect password tries
  75. } __attribute__ ((packed));
  76. /**
  77. * Chip information structure
  78. */
  79. #define U3_MAX_CHIP_REVISION_LEN 8
  80. #define U3_MAX_CHIP_MANUFACTURER_LEN 16
  81. struct chip_info {
  82. char revision[U3_MAX_CHIP_REVISION_LEN];
  83. char manufacturer[U3_MAX_CHIP_MANUFACTURER_LEN];
  84. } __attribute__ ((packed));
  85. /********************************** functions *********************************/
  86. /**
  87. * Read device property page
  88. *
  89. * This reads a page of device properties from the device. A requested page is
  90. * stored, including there page header, in 'buffer'. If the buffer size, as
  91. * specified by 'buffer_length', is to small to store the whole page, only the
  92. * first 'buffer_length' bytes are stored. The caller should check if the full
  93. * page fitted into the buffer, by comparing the page length in the header with
  94. * the buffer length. If the function returns successfull then atleast
  95. * sizeof(struct property_header) bytes will be valid in the buffer, therefore
  96. * 'buffer_length should be atleast 6 byte. If a property page does not excist,
  97. * this function will fail.
  98. *
  99. * @param device U3 device handle
  100. * @param property_id The index of the requested property page
  101. * @param buffer The buffer to return the requested property page in.
  102. * @param buffer_length The allocated length of 'buffer' in bytes.
  103. *
  104. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  105. * an error string can be obtained using u3_error()
  106. *
  107. * @see 'struct property_header', 'struct property_*'
  108. */
  109. int u3_read_device_property(u3_handle_t *device, uint16_t property_id,
  110. uint8_t *buffer, uint16_t buffer_length);
  111. /**
  112. * Repartiton device
  113. *
  114. * This repatitions the device into a CD and a data partition. All current data
  115. * will be lost. Creating a CD partition of size 0 will make this device an
  116. * ordenary USB stick.
  117. *
  118. * @param device U3 device handle
  119. * @param cd_size The size of the CD partition in 512 byte sectors.
  120. *
  121. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  122. * an error string can be obtained using u3_error()
  123. */
  124. int u3_partition(u3_handle_t *device, uint32_t cd_size);
  125. /**
  126. * Write CD block
  127. *
  128. * This function write's a block to the CD partition. Blocks are 2048 bytes in
  129. * size.
  130. *
  131. * @param device U3 device handle
  132. * @param block_num The block number to write
  133. * @param block A pointer to buffer containing block
  134. *
  135. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  136. * an error string can be obtained using u3_error()
  137. */
  138. int u3_cd_write(u3_handle_t *device, uint32_t block_num, uint8_t *block);
  139. /**
  140. * Direction to round sector count.
  141. *
  142. * @see 'u3_sector_round()'
  143. */
  144. enum round_dir { round_down=0, round_up=1 };
  145. /**
  146. * Round partition sector count to prevered value
  147. *
  148. * Round a number of sectors to a sector count that is allowed for partitioning
  149. * the device.
  150. *
  151. * @param device U3 device handle
  152. * @param direction direction to round(up or down).
  153. * @param size Size value to round. Rounded value is writen back to
  154. * this variable.
  155. *
  156. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  157. * an error string can be obtained using u3_error()
  158. *
  159. * @see 'u3_partition()'
  160. */
  161. int u3_partition_sector_round(u3_handle_t *device,
  162. enum round_dir direction, uint32_t *size);
  163. /**
  164. * Round secure zone sector count to prevered value
  165. *
  166. * Round a number of sectors to a sector count that is allowed for a secure
  167. * zone on the data partition.
  168. *
  169. * @param device U3 device handle
  170. * @param direction direction to round(up or down).
  171. * @param size Size value to round. Rounded value is writen back to
  172. * this variable.
  173. *
  174. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  175. * an error string can be obtained using u3_error()
  176. *
  177. * @see 'u3_enable_security()'
  178. */
  179. int u3_security_sector_round(u3_handle_t *device,
  180. enum round_dir direction, uint32_t *size);
  181. /**
  182. * Request partitioning information
  183. *
  184. * This request some information about the partition sizes
  185. *
  186. * @param device U3 device handle
  187. * @param info Pointer to structure used to return requested info.
  188. *
  189. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  190. * an error string can be obtained using u3_error()
  191. *
  192. * @see 'struct part_info'
  193. */
  194. int u3_partition_info(u3_handle_t *device, struct part_info *info);
  195. /**
  196. * Request data partition information
  197. *
  198. * This request some information about the data partition
  199. *
  200. * @param device U3 device handle
  201. * @param info Pointer to structure used to return requested info.
  202. *
  203. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  204. * an error string can be obtained using u3_error()
  205. *
  206. * @see 'struct dpart_info'
  207. */
  208. int u3_data_partition_info(u3_handle_t *device, struct dpart_info *info);
  209. /**
  210. * Request chip information
  211. *
  212. * This request some information about the USB chip used on the device.
  213. *
  214. * @param device U3 device handle
  215. * @param info Pointer to structure used to return requested info.
  216. *
  217. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  218. * an error string can be obtained using u3_error()
  219. *
  220. * @see 'struct chip_info'
  221. */
  222. int u3_chip_info(u3_handle_t *device, struct chip_info *info);
  223. /**
  224. * Reset device
  225. *
  226. * This function tell's the device to reconnect. When the function returns
  227. * the device should have been reset and reconnected.
  228. * The exact working of this action is still vague
  229. *
  230. * @param device U3 device handle
  231. *
  232. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  233. * an error string can be obtained using u3_error()
  234. */
  235. int u3_reset(u3_handle_t *device);
  236. /**
  237. * Enable device security
  238. *
  239. * This sets a password on the data partition.
  240. *
  241. * @param device U3 device handle
  242. * @param password The password for the private zone
  243. *
  244. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  245. * an error string can be obtained using u3_error()
  246. */
  247. int u3_enable_security(u3_handle_t *device, const char *password);
  248. /**
  249. * Disable device security
  250. *
  251. * This removes the password from the data partition.
  252. *
  253. * @param device U3 device handle
  254. * @param password The password for the private zone
  255. * @param result Return variable for result True if unlocking succeeded,
  256. * else false
  257. *
  258. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  259. * an error string can be obtained using u3_error()
  260. */
  261. int u3_disable_security(u3_handle_t *device, const char *password,
  262. int * result);
  263. /**
  264. * Unlock data partition
  265. *
  266. * This unlocks the data partition if the device is secured.
  267. *
  268. * @param device U3 device handle
  269. * @param password The password for the private zone
  270. * @param result Return variable for result True if unlocking succeeded,
  271. * else false
  272. *
  273. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  274. * an error string can be obtained using u3_error()
  275. */
  276. int u3_unlock(u3_handle_t *device, const char *password, int *result);
  277. /**
  278. * Change password of data partition
  279. *
  280. * This changes the password of the data partition.
  281. *
  282. * @param device U3 device handle
  283. * @param old_password The current password of the private zone
  284. * @param new_password The new password of the private zone
  285. * @param result Return variable for result: True if password changed,
  286. * else False if old password incorrect.
  287. *
  288. * @returns U3_SUCCESS if successful, else U3_FAILURE and
  289. * an error string can be obtained using u3_error()
  290. */
  291. int u3_change_password(u3_handle_t *device, const char *old_password,
  292. const char *new_password, int *result);
  293. #endif // __U3_COMMAND__