aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 112dc2ea83d38707cd8fc89a01882a5387518f60 (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
#include <windows.h>
#include <stdio.h>

BOOL WINAPI VirtualCopy(LPVOID lpvDest, LPVOID lpvSrc, DWORD cbSize, DWORD fdwProtect);

enum {
  PAGESIZE = 0x10000,
};

static BOOL writefile(const void *p, unsigned size, const wchar_t *fname)
{
  FILE *f = 0;
  const char *pb = p;
  BOOL ret = FALSE;

  f = _wfopen(fname, L"wb");
  if (!f) {
    wprintf(L"\nError: fopen failed! Aborting.\n");
    goto cleanup;
  }
  while (size) {
    const unsigned wsize = (size < PAGESIZE) ? size : PAGESIZE;
    if (fwrite(pb, 1, wsize, f) != wsize) {
      wprintf(L"\nError: fwrite failed! Aborting.\n");
      goto cleanup;
    }
    wprintf(L".");
    fflush(stdout);
    pb += wsize;
    size -= wsize;
  }
  ret = TRUE;
cleanup:
  if (f) fclose(f);
  return ret;
}


struct data {
  unsigned addr;
  unsigned size;
  const wchar_t *name;
};

const struct data roms[] = {
  {0x00000000, 0x02000000, L"rom.dat"},  // 0x0000_0000 - 0x01ff_ffff
  {0x0c000000, 0x00400000, L"flash.dat"}, // 0x0c00_0000 - 0x0c3f_ffff
  {0, 0, 0},
};

int wmain(int argc, wchar_t **argv)
{
  wchar_t fname[MAX_PATH];
  wchar_t *pathend;
  const struct data *i;

  if (!GetModuleFileName(0, fname, MAX_PATH)) {
    wprintf(L"Error: Cannot get executable file path! Aborting.\n");
    goto end;
  }
  pathend = wcsrchr(fname, L'\\');
  if (!pathend) {
    wprintf(L"Error: Cannot get directory to write rom files! Aborting\n");
    goto end;
  }
  for (i = roms; i->name; i++) {
    void *p;

    /* too lazy to check string length */
    pathend[1] = 0;
    wcscat(fname, i->name);
    wprintf(L"Dumping %ls", fname);
    fflush(stdout);
    p = VirtualAlloc(0, i->size, MEM_RESERVE, PAGE_NOACCESS);
    if (!p) {
      wprintf(L"\nError: VirtualAlloc failed! Aborting.\n");
      goto end;
    }
    if (!VirtualCopy(p, (void *)(i->addr >> 8), i->size, PAGE_READONLY | PAGE_PHYSICAL)) {
      wprintf(L"\nError: VirtualCopy failed! Aborting.\n");
      goto end;
    }
    if (!writefile(p, i->size, fname)) return 1;
    wprintf(L"OK!\n");
  }
end:
  wprintf(L"Press enter to continue...\n");
  getwchar();
  return 0;
}