Advertisement
Bond697

lpkm card

Aug 1st, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.57 KB | None | 0 0
  1. #ifndef  CARD_H
  2. #define  CARD_H
  3.  
  4. #include <nds.h>
  5. #include <types.h>
  6. #include <os_card.h>
  7.  
  8.  
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12.  
  13. /*-----------------------------------------------------------------------------
  14. -- macros
  15. -----------------------------------------------------------------------------*/
  16.  
  17. #define MAX_CARD_RETRIES                10
  18. #define LPKM_BACKUP_TYPE_4MBIT_FLASH    0xFF1302
  19.  
  20. /*-----------------------------------------------------------------------------
  21. -- type definitions
  22. -----------------------------------------------------------------------------*/
  23.  
  24. typedef void (*CardCallbackFn)(void*);
  25.  
  26.  
  27. /*-----------------------------------------------------------------------------
  28. -- enums
  29. -----------------------------------------------------------------------------*/
  30.  
  31. typedef enum CardResult {
  32.  
  33.   RESULT_SUCCESS        = 0,
  34.   RESULT_FAIL           = 1,
  35.   RESULT_INVALID_PARAM  = 2,
  36.   RESULT_UNSUPPORTED    = 3,
  37.   RESULT_TIMEOUT        = 4,
  38.   RESULT_ERROR          = 5,
  39.   RESULT_NO_RESPONSE    = 6,
  40.   RESULT_CANCELED       = 7
  41.  
  42. } CardResult;
  43.  
  44.  
  45. typedef enum CardRequest {
  46.  
  47.     REQUEST_INIT                    = 0,                
  48.     REQUEST_ACK                     = 1,    
  49.     REQUEST_IDENTIFY                = 2,                
  50.     REQUEST_READ_ID                 = 3,                  
  51.     REQUEST_READ_ROM                = 4,                
  52.     REQUEST_WRITE_ROM               = 5,              
  53.     REQUEST_READ_BACKUP             = 6,              
  54.     REQUEST_WRITE_BACKUP            = 7,            
  55.     REQUEST_PROGRAM_BACKUP          = 8,          
  56.     REQUEST_VERIFY_BACKUP           = 9,            
  57.     REQUEST_ERASE_PAGE_BACKUP       = 10,      
  58.     REQUEST_ERASE_SECTOR_BACKUP     = 11,      
  59.     REQUEST_ERASE_CHIP_BACKUP       = 12,      
  60.     REQUEST_READ_STATUS             = 13,              
  61.     REQUEST_WRITE_STATUS            = 14,            
  62.     REQUEST_ERASE_SUBSECTOR_BACKUP  = 15,  
  63.     REQUEST_MAX                     = 16
  64.  
  65. } CardRequest;
  66.  
  67.  
  68. typedef enum CardRequestMode {
  69.  
  70.     REQUEST_MODE_RECV           = 0,            
  71.     REQUEST_MODE_SEND           = 1,            
  72.     REQUEST_MODE_SEND_VERIFY    = 2,    
  73.     REQUEST_MODE_SPECIAL        = 3      
  74.  
  75. } CardRequestMode;
  76.  
  77.  
  78. /*-----------------------------------------------------------------------------
  79. -- structs
  80. -----------------------------------------------------------------------------*/
  81.  
  82. typedef struct RomRegion {
  83.  
  84.     u32     offset;
  85.     u32     length;
  86.  
  87. } RomRegion;
  88.  
  89.  
  90. /*-----------------------------------------------------------------------------
  91. -- types
  92. -----------------------------------------------------------------------------*/
  93.  
  94.  
  95.  
  96.  
  97. /*-----------------------------------------------------------------------------
  98. -- inline Functions
  99. -----------------------------------------------------------------------------*/
  100.  
  101. _INLINE bool ReadCardBackupHw(u32 src, void* dest, u32 size, CardCallbackFn cb, void* cb_arg, bool is_async)
  102. {
  103.     return CARDi_RequestStreamCommand((u32)src, (u32)dest, size, cb, cb_arg, is_async, REQUEST_READ_BACKUP, 1, REQUEST_MODE_RECV);
  104. }
  105.  
  106.  
  107. /*
  108. ReadSave args:
  109.  
  110. src:    offset to start reading at in the save- 0 if you're reading from the top
  111. dest:   buffer to read into
  112. size:   read size
  113. cb:     callback function- this is the function that will be run if the save read completes successfully. use NULL if you don't need one.
  114. cb_arg: callback function argument- ptr to the argument for the callback function.  also NULL if unused.
  115. */
  116. _INLINE bool ReadSave(u32 src, void* dest, u32 size, CardCallbackFn cb, void* cb_arg)  
  117. {
  118.     return ReadCardBackupHw(src, dest, size, cb, cb_arg, TRUE);
  119. }
  120.  
  121. /*
  122. to use:
  123.  
  124. bool g_save_read = FALSE;
  125.  
  126. void SetSavReadFlag()
  127. {
  128.     save_read = TRUE;
  129. }
  130.  
  131. const char* sav_src = "save_src.cpp";
  132. void* save_buf = allocateBlockFromExpHeap(GRP_ID_SAV_BLK, BLK_LEN, TRUE, sav_src, SAV_BLK_LOAD_MK);
  133.  
  134. bool save_result = ReadSave(0 + block_address_you_use_if_any, save_buf, BLK_LEN, SetSavReadFlag, NULL);
  135.  
  136. */
  137.  
  138.  
  139. //-----------------------------------------------------------------------------
  140. //-----------------------------------------------------------------------------
  141.  
  142. _INLINE void WriteCardBackupHw(u32 dest, const void* src, u32 size, CardCallbackFn cb, void* cb_arg, bool is_async)
  143. {
  144.     CARDi_RequestStreamCommand((u32)src, (u32)dest, size, cb, cb_arg, is_async, REQUEST_WRITE_BACKUP, MAX_CARD_RETRIES, REQUEST_MODE_SEND_VERIFY);
  145. }
  146.  
  147.  
  148. /*
  149. WriteSave args:
  150.  
  151. dest:   offset into the save to write
  152. src:    address in memory to start writing from
  153. size:   write size
  154. cb:     callback function- this is the function that will be run if the save read completes successfully. use NULL if you don't need one.
  155. cb_arg: callback function argument- ptr to the argument for the callback function.  also NULL if unused.
  156. */
  157. _INLINE void WriteSave(u32 dest, const void* src, u32 size, CallbackFn cb, void* cb_arg)
  158. {
  159.     WriteCardBackupHw(dest, src, size, cb, cb_arg, TRUE);
  160. }
  161.  
  162.  
  163. /*-----------------------------------------------------------------------------
  164. -- function declarations
  165. -----------------------------------------------------------------------------*/
  166.  
  167. bool CARD_IsEnabled();
  168. void CARD_CheckEnabled();
  169. void CARD_Enable(bool enable);
  170. CardResult CARD_GetResultCode();
  171. const u8* CARD_GetRomHeader();
  172. void CARD_LockRom(u16 lock_id);     // lock access to card rom - you call OS_GetLockID first to get the lock id to use here, lock_id is basically the owner of the current lock/unlock action
  173. void CARD_UnlockRom(u16 lock_id);   // once you unlock access, use the same os lock id
  174. void CARD_LockBackup(u16 lock_id);  // lock access to card backup - call OS_GetLockID first
  175. void CARD_UnlockBackup(u16 lock_id);
  176. bool CARDi_RequestStreamCommand(u32 src, u32 dst, u32 len, CallbackFn callback, void *arg, BOOL is_async, CardRequest req_type, int req_retry, CardRequestMode req_mode);
  177. bool CARD_IdentifyBackup(u32 backup_type);  // you CAN use this, but you don't have to.  the type of backup is pre-identified by gamefreak.
  178. u32  CARD_GetBackupSectorSize();
  179. bool CARD_WaitBackupAsync();
  180. bool CARD_TryWaitBackupAsync();
  181. void CARD_CancelBackupAsync();  // kill the currently running async process for the card
  182. void CARD_WaitRomAsync();
  183. bool CARD_IsPulledOut();
  184.  
  185.  
  186. #ifdef __cplusplus
  187. } /* extern "C"*/
  188. #endif
  189.  
  190. #endif // CARD_H
  191.  
  192.  
  193. /*
  194. full example of reading from a save:
  195.  
  196. bool g_save_read = FALSE;
  197.  
  198. void ReadInSavBlk(u32 read_size, void* data_dest, u32 read_offset)
  199. {
  200.  
  201. s32 id = OS_GetLockID();
  202.  
  203. if(id == LOCK_ID_ERR)
  204. {
  205.     assert_fail();
  206. }
  207.  
  208. CARD_LockBackup((u16)id);
  209.  
  210. UnsetSavReadFlag();
  211. ReadSave(read_offset, data_dest, read_size, SetSavReadFlag, NULL);
  212.  
  213. CARD_UnlockBackup((u16)id);
  214. OS_ReleaseLockID((u16)id);
  215. }
  216.  
  217.  
  218. void SetSavReadFlag()
  219. {
  220.     save_read = TRUE;
  221. }
  222.  
  223.  
  224. void UnsetSavReadFlag()
  225. {
  226.     save_read = FALSE;
  227. }
  228. */
  229.  
  230. // NOTE: you do not have to use a callback.  this is just a demonstration of how to.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement