libwbfs.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef LIBWBFS_H
  2. #define LIBWBFS_H
  3. #include "libwbfs_os.h" // this file is provided by the project wanting to compile libwbfs
  4. #include "wiidisc.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif /* __cplusplus */
  8. typedef u32 be32_t;
  9. typedef u16 be16_t;
  10. #ifdef WIN32
  11. #pragma pack(1)
  12. #endif
  13. typedef struct wbfs_head
  14. {
  15. be32_t magic;
  16. // parameters copied in the partition for easy dumping, and bug reports
  17. be32_t n_hd_sec; // total number of hd_sec in this partition
  18. u8 hd_sec_sz_s; // sector size in this partition
  19. u8 wbfs_sec_sz_s; // size of a wbfs sec
  20. u8 padding3[2];
  21. u8 disc_table[0]; // size depends on hd sector size
  22. }
  23. #ifndef WIN32
  24. __attribute((packed)) wbfs_head_t;
  25. #else
  26. __attribute((packed)) wbfs_head_t;
  27. #endif
  28. #ifdef WIN32
  29. #pragma pack()
  30. #endif
  31. typedef struct wbfs_disc_info
  32. {
  33. u8 disc_header_copy[0x100];
  34. be16_t wlba_table[0];
  35. }wbfs_disc_info_t;
  36. // WBFS first wbfs_sector structure:
  37. //
  38. // -----------
  39. // | wbfs_head | (hd_sec_sz)
  40. // -----------
  41. // | |
  42. // | disc_info |
  43. // | |
  44. // -----------
  45. // | |
  46. // | disc_info |
  47. // | |
  48. // -----------
  49. // | |
  50. // | ... |
  51. // | |
  52. // -----------
  53. // | |
  54. // | disc_info |
  55. // | |
  56. // -----------
  57. // | |
  58. // |freeblk_tbl|
  59. // | |
  60. // -----------
  61. //
  62. // callback definition. Return 1 on fatal error (callback is supposed to make retries until no hopes..)
  63. typedef int (*rw_sector_callback_t)(void*fp,u32 lba,u32 count,void*iobuf);
  64. typedef void (*progress_callback_t)(int status,int total);
  65. #ifdef WIN32
  66. typedef void (*close_callback_t)(void*fp);
  67. #endif
  68. typedef struct wbfs_s
  69. {
  70. wbfs_head_t *head;
  71. /* hdsectors, the size of the sector provided by the hosting hard drive */
  72. u32 hd_sec_sz;
  73. u8 hd_sec_sz_s; // the power of two of the last number
  74. u32 n_hd_sec; // the number of hd sector in the wbfs partition
  75. /* standard wii sector (0x8000 bytes) */
  76. u32 wii_sec_sz;
  77. u8 wii_sec_sz_s;
  78. u32 n_wii_sec;
  79. u32 n_wii_sec_per_disc;
  80. /* The size of a wbfs sector */
  81. u32 wbfs_sec_sz;
  82. u32 wbfs_sec_sz_s;
  83. u16 n_wbfs_sec; // this must fit in 16 bit!
  84. u16 n_wbfs_sec_per_disc; // size of the lookup table
  85. u32 part_lba;
  86. /* virtual methods to read write the partition */
  87. rw_sector_callback_t read_hdsector;
  88. rw_sector_callback_t write_hdsector;
  89. #ifdef WIN32
  90. close_callback_t close_hd;
  91. #endif
  92. void *callback_data;
  93. u16 max_disc;
  94. u32 freeblks_lba;
  95. u32 *freeblks;
  96. u16 disc_info_sz;
  97. u8 *tmp_buffer; // pre-allocated buffer for unaligned read
  98. u32 n_disc_open;
  99. }wbfs_t;
  100. typedef struct wbfs_disc_s
  101. {
  102. wbfs_t *p;
  103. wbfs_disc_info_t *header; // pointer to wii header
  104. int i; // disc index in the wbfs header (disc_table)
  105. }wbfs_disc_t;
  106. #define WBFS_MAGIC (('W'<<24)|('B'<<16)|('F'<<8)|('S'))
  107. /*! @brief open a MSDOS partitionned harddrive. This tries to find a wbfs partition into the harddrive
  108. @param read_hdsector,write_hdsector: accessors to a harddrive
  109. @hd_sector_size: size of the hd sector. Can be set to zero if the partition in already initialized
  110. @num_hd_sector: number of sectors in this disc. Can be set to zero if the partition in already initialized
  111. @reset: not implemented, This will format the whole harddrive with one wbfs partition that fits the whole disk.
  112. calls wbfs_error() to have textual meaning of errors
  113. @return NULL in case of error
  114. */
  115. wbfs_t * wbfs_open_hd(rw_sector_callback_t read_hdsector,
  116. rw_sector_callback_t write_hdsector,
  117. #ifdef WIN32
  118. close_callback_t close_hd,
  119. #endif
  120. void *callback_data,
  121. int hd_sector_size, int num_hd_sector, int reset);
  122. /*! @brief open a wbfs partition
  123. @param read_hdsector,write_hdsector: accessors to the partition
  124. @hd_sector_size: size of the hd sector. Can be set to zero if the partition in already initialized
  125. @num_hd_sector: number of sectors in this partition. Can be set to zero if the partition in already initialized
  126. @partition_lba: The partitio offset if you provided accessors to the whole disc.
  127. @reset: initialize the partition with an empty wbfs.
  128. calls wbfs_error() to have textual meaning of errors
  129. @return NULL in case of error
  130. */
  131. wbfs_t*wbfs_open_partition(rw_sector_callback_t read_hdsector,
  132. rw_sector_callback_t write_hdsector,
  133. #ifdef WIN32
  134. close_callback_t close_hd,
  135. #endif
  136. void *callback_data,
  137. int hd_sector_size, int num_hd_sector, u32 partition_lba, int reset);
  138. /*! @brief close a wbfs partition, and sync the metadatas to the disc */
  139. void wbfs_close(wbfs_t*);
  140. /*! @brief open a disc inside a wbfs partition use a 6 char discid+vendorid
  141. @return NULL if discid is not present
  142. */
  143. wbfs_disc_t *wbfs_open_disc(wbfs_t* p, u8 *diskid);
  144. /*! @brief close a already open disc inside a wbfs partition */
  145. void wbfs_close_disc(wbfs_disc_t*d);
  146. /*! @brief accessor to the wii disc
  147. @param d: a pointer to already open disc
  148. @param offset: an offset inside the disc, *points 32bit words*, allowing to access 16GB data
  149. @param len: The length of the data to fetch, in *bytes*
  150. */
  151. // offset is pointing 32bit words to address the whole dvd, although len is in bytes
  152. int wbfs_disc_read(wbfs_disc_t*d,u32 offset, u8 *data, u32 len);
  153. /*! @return the number of discs inside the paritition */
  154. u32 wbfs_count_discs(wbfs_t*p);
  155. /*! get the disc info of ith disc inside the partition. It correspond to the first 0x100 bytes of the wiidvd
  156. http://www.wiibrew.org/wiki/Wiidisc#Header
  157. @param i: index of the disc inside the partition
  158. @param header: pointer to 0x100 bytes to write the header
  159. @size: optional pointer to a 32bit word that will get the size in 32bit words of the DVD taken on the partition.
  160. */
  161. u32 wbfs_get_disc_info(wbfs_t*p, u32 i,u8 *header,int header_size,u32 *size);
  162. /*! get the number of used block of the partition.
  163. to be multiplied by p->wbfs_sec_sz (use 64bit multiplication) to have the number in bytes
  164. */
  165. u32 wbfs_count_usedblocks(wbfs_t*p);
  166. /******************* write access ******************/
  167. /*! add a wii dvd inside the partition
  168. @param read_src_wii_disc: a callback to access the wii dvd. offsets are in 32bit, len in bytes!
  169. @callback_data: private data passed to the callback
  170. @spinner: a pointer to a function that is regulary called to update a progress bar.
  171. @sel: selects which partitions to copy.
  172. @copy_1_1: makes a 1:1 copy, whenever a game would not use the wii disc format, and some data is hidden outside the filesystem.
  173. #ifdef WIN32
  174. // It's a bit silly to fidef this... - g3power
  175. @new_name: different name for imported ISO. NULL to use default name from ISO header
  176. #endif
  177. */
  178. u32 wbfs_add_disc(wbfs_t*p,read_wiidisc_callback_t read_src_wii_disc,
  179. void *callback_data,
  180. progress_callback_t spinner,
  181. partition_selector_t sel,
  182. int copy_1_1
  183. #ifdef WIN32
  184. , char *new_name
  185. #endif
  186. );
  187. u32 wbfs_estimate_disc(wbfs_t*p,read_wiidisc_callback_t read_src_wii_disc, void *callback_data,
  188. partition_selector_t sel, u8* header);
  189. /*! remove a wiidvd inside a partition */
  190. u32 wbfs_rm_disc(wbfs_t*p, u8* discid);
  191. /*! rename a wiidvd inside a partition */
  192. u32 wbfs_ren_disc(wbfs_t*p, u8* discid, u8* newname);
  193. /*! edit a wiidvd diskid */
  194. u32 wbfs_nid_disc(wbfs_t*p, u8* discid, u8* newid);
  195. /*! trim the file-system to its minimum size
  196. This allows to use wbfs as a wiidisc container
  197. */
  198. u32 wbfs_trim(wbfs_t*p);
  199. /*! extract a disc from the wbfs, unused sectors are just untouched, allowing descent filesystem to only really usefull space to store the disc.
  200. Even if the filesize is 4.7GB, the disc usage will be less.
  201. */
  202. u32 wbfs_extract_disc(wbfs_disc_t*d, rw_sector_callback_t write_dst_wii_sector,void *callback_data,progress_callback_t spinner);
  203. /*! extract a file from the wii disc filesystem.
  204. E.G. Allows to extract the opening.bnr to install a game as a system menu channel
  205. */
  206. u32 wbfs_extract_file(wbfs_disc_t*d, char *path);
  207. // remove some sanity checks
  208. void wbfs_set_force_mode(int force);
  209. /* OS specific functions provided by libwbfs_<os>.c */
  210. wbfs_t *wbfs_try_open(char *disk, char *partition, int reset);
  211. wbfs_t *wbfs_try_open_partition(char *fn, int reset);
  212. void *wbfs_open_file_for_read(char*filename);
  213. void *wbfs_open_file_for_write(char*filename);
  214. int wbfs_read_file(void*handle, int len, void *buf);
  215. int wbfs_close_file(void *handle);
  216. int wbfs_file_reserve_space(void*handle,long long size);
  217. void wbfs_file_truncate(void *handle,long long size);
  218. int wbfs_read_wii_file(void *handle, u32 offset, u32 count, void *buf);
  219. int wbfs_write_wii_file(void *handle, u32 lba, u32 count, void *buf);
  220. #ifdef __cplusplus
  221. }
  222. #endif /* __cplusplus */
  223. #endif