summaryrefslogtreecommitdiff
path: root/src/Dolphin/card/CARDDir.c
blob: 1daecf8ef95111de91ec1fce35a33613d55ee194 (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
#include <dolphin/card.h>
#include <dolphin/dsp.h>
#include <dolphin/dvd.h>
#include <dolphin/os.h>

#include <dolphin/CARDPriv.h>

CARDDir* __CARDGetDirBlock(CARDControl* card) { return card->currentDir; }

static void WriteCallback(s32 chan, s32 result) {
  CARDControl* card;
  CARDCallback callback;

  card = &__CARDBlock[chan];
  if (0 <= result) {
    CARDDir* dir0 = (CARDDir*)((u8*)card->workArea + 0x2000);
    CARDDir* dir1 = (CARDDir*)((u8*)card->workArea + 0x4000);

    if (card->currentDir == dir0) {
      card->currentDir = dir1;
      memcpy(dir1, dir0, 0x2000);
    } else {
      card->currentDir = dir0;
      memcpy(dir0, dir1, 0x2000);
    }
  }

error:
  if (card->apiCallback == 0) {
    __CARDPutControlBlock(card, result);
  }
  callback = card->eraseCallback;
  if (callback) {
    card->eraseCallback = 0;
    callback(chan, result);
  }
}

static void EraseCallback(s32 chan, s32 result) {
  CARDControl* card;
  CARDCallback callback;
  CARDDir* dir;
  u32 tmp[2];
  u32 addr;

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

  dir = __CARDGetDirBlock(card);
  addr = ((u32)dir - (u32)card->workArea) / 0x2000 * card->sectorSize;
  result = __CARDWrite(chan, addr, 0x2000, dir, WriteCallback);
  if (result < 0) {
    goto error;
  }

  return;

error:
  if (card->apiCallback == 0) {
    __CARDPutControlBlock(card, result);
  }
  callback = card->eraseCallback;
  if (callback) {
    card->eraseCallback = 0;
    callback(chan, result);
  }
}

s32 __CARDUpdateDir(s32 chan, CARDCallback callback) {
  CARDControl* card;
  CARDDirCheck* check;
  u32 tmp[2];
  u32 addr;
  CARDDir* dir;

  card = &__CARDBlock[chan];
  if (!card->attached) {
    return CARD_RESULT_NOCARD;
  }

  dir = __CARDGetDirBlock(card);
  check = __CARDGetDirCheck(dir);
  ++check->checkCode;
  __CARDCheckSum(dir, 0x2000 - sizeof(u32), &check->checkSum, &check->checkSumInv);
  DCStoreRange(dir, 0x2000);

  card->eraseCallback = callback;
  addr = ((u32)dir - (u32)card->workArea) / 0x2000 * card->sectorSize;
  return __CARDEraseSector(chan, addr, EraseCallback);
}