summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDCreate.c
blob: 6802b91e365e5bc79bf74bcaad3a56c948c532b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <dolphin/card.h>
#include <dolphin/dsp.h>
#include <dolphin/dvd.h>
#include <dolphin/os.h>

#include <dolphin/CARDPriv.h>

static void CreateCallbackFat(s32 chan, s32 result) {
  CARDControl* card;
  CARDDir* dir;
  CARDDir* ent;
  CARDCallback callback;

  card = &__CARDBlock[chan];
  callback = card->apiCallback;
  card->apiCallback = 0;
  if (result < 0) {
    goto error;
  }

  dir = __CARDGetDirBlock(card);
  ent = &dir[card->freeNo];
  memcpy(ent->gameName, card->diskID->gameName, sizeof(ent->gameName));
  memcpy(ent->company, card->diskID->company, sizeof(ent->company));
  ent->permission = CARD_ATTR_PUBLIC;
  ent->copyTimes = 0;
  ent->startBlock = card->startBlock;

  ent->bannerFormat = 0;
  ent->iconAddr = 0xffffffff;
  ent->iconFormat = 0;
  ent->iconSpeed = 0;
  ent->commentAddr = 0xffffffff;

  CARDSetIconSpeed(ent, 0, CARD_STAT_SPEED_FAST);

  card->fileInfo->offset = 0;
  card->fileInfo->iBlock = ent->startBlock;

  ent->time = (u32)OSTicksToSeconds(OSGetTime());
  result = __CARDUpdateDir(chan, callback);
  if (result < 0) {
    goto error;
  }
  return;

error:
  __CARDPutControlBlock(card, result);
  if (callback) {
    callback(chan, result);
  }
}

s32 CARDCreateAsync(s32 chan, const char* fileName, u32 size, CARDFileInfo* fileInfo,
                    CARDCallback callback) {
  CARDControl* card;
  CARDDir* dir;
  CARDDir* ent;
  s32 result;
  u16 fileNo;
  u16 freeNo;
  u16* fat;

  if (strlen(fileName) > (u32)CARD_FILENAME_MAX) {
    return CARD_RESULT_NAMETOOLONG;
  }

  result = __CARDGetControlBlock(chan, &card);
  if (result < 0) {
    return result;
  }

  if (size <= 0 || (size % card->sectorSize) != 0) {
    return CARD_RESULT_FATAL_ERROR;
  }

  freeNo = (u16)-1;
  dir = __CARDGetDirBlock(card);
  for (fileNo = 0; fileNo < CARD_MAX_FILE; fileNo++) {
    ent = &dir[fileNo];
    if (ent->gameName[0] == 0xff) {
      if (freeNo == (u16)-1) {
        freeNo = fileNo;
      }
    } else if (memcmp(ent->gameName, card->diskID->gameName, sizeof(ent->gameName)) == 0 &&
               memcmp(ent->company, card->diskID->company, sizeof(ent->company)) == 0 &&
               __CARDCompareFileName(ent, fileName)) {
      return __CARDPutControlBlock(card, CARD_RESULT_EXIST);
    }
  }
  if (freeNo == (u16)-1) {
    return __CARDPutControlBlock(card, CARD_RESULT_NOENT);
  }

  fat = __CARDGetFatBlock(card);
  if (card->sectorSize * fat[CARD_FAT_FREEBLOCKS] < size) {
    return __CARDPutControlBlock(card, CARD_RESULT_INSSPACE);
  }

  card->apiCallback = callback ? callback : __CARDDefaultApiCallback;
  card->freeNo = freeNo;
  ent = &dir[freeNo];
  ent->length = (u16)(size / card->sectorSize);
  strncpy(ent->fileName, fileName, CARD_FILENAME_MAX);

  card->fileInfo = fileInfo;
  fileInfo->chan = chan;
  fileInfo->fileNo = freeNo;

  result = __CARDAllocBlock(chan, size / card->sectorSize, CreateCallbackFat);
  if (result < 0) {
    return __CARDPutControlBlock(card, result);
  }
  return result;
}