Skip to content

Commit b974c0b

Browse files
committed
Refactor and consolidate DLDI driver patching code
1 parent d81559d commit b974c0b

File tree

5 files changed

+208
-261
lines changed

5 files changed

+208
-261
lines changed

arm9/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TOPDIR ?= $(CURDIR)/..
1818
# all directories are relative to this makefile
1919
#---------------------------------------------------------------------------------
2020
BUILD ?= release
21-
SOURCES := ../source/arm9 ../source/arm9/dldi ../source/arm9/system ../source/common
21+
SOURCES := ../source/arm9 ../source/arm9/system ../source/common
2222
INCLUDES := ../include ../source/common
2323
GRAPHICS := ../source/arm9/gfx
2424
DATA := ../source/arm9

include/nds/arm9/dldi.h

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
dldi.h
33
44
Copyright (c) 2006 Michael "Chishm" Chisholm
5-
5+
66
Redistribution and use in source and binary forms, with or without modification,
77
are permitted provided that the following conditions are met:
88
@@ -25,80 +25,9 @@
2525
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
*/
2727

28-
2928
#ifndef NDS_DLDI_INCLUDE
3029
#define NDS_DLDI_INCLUDE
3130

32-
#ifdef __cplusplus
33-
extern "C" {
34-
#endif
35-
36-
#include "../disc_io.h"
37-
#include <calico/dev/dldi.h>
38-
39-
#define FIX_ALL DLDI_FIX_ALL
40-
#define FIX_GLUE DLDI_FIX_GLUE
41-
#define FIX_GOT DLDI_FIX_GOT
42-
#define FIX_BSS DLDI_FIX_BSS
43-
44-
extern const u32 DLDI_MAGIC_NUMBER;
45-
46-
// I/O interface with DLDI extensions
47-
typedef struct DLDI_INTERFACE {
48-
u32 magicNumber;
49-
char magicString [DLDI_MAGIC_STRING_LEN];
50-
u8 versionNumber;
51-
u8 driverSize; // log-2 of driver size in bytes
52-
u8 fixSectionsFlags;
53-
u8 allocatedSize; // log-2 of the allocated space in bytes
54-
55-
char friendlyName [DLDI_FRIENDLY_NAME_LEN];
56-
57-
// Pointers to sections that need address fixing
58-
void* dldiStart;
59-
void* dldiEnd;
60-
void* interworkStart;
61-
void* interworkEnd;
62-
void* gotStart;
63-
void* gotEnd;
64-
void* bssStart;
65-
void* bssEnd;
66-
67-
// Original I/O interface data
68-
DISC_INTERFACE ioInterface;
69-
} DLDI_INTERFACE;
70-
71-
/*
72-
Determines if an IO driver is a valid DLDI driver
73-
*/
74-
extern bool dldiIsValid (const DLDI_INTERFACE* io);
75-
76-
/*
77-
Adjust the pointer addresses within a DLDI driver
78-
*/
79-
extern void dldiFixDriverAddresses (DLDI_INTERFACE* io);
80-
81-
/*
82-
Load a DLDI from disc and set up the bus permissions.
83-
This returns a type not directly usable in libfat,
84-
but it does give extra information, such as a friendly name.
85-
To use in libfat:
86-
const DLDI_INTERFACE* loadedDldi = dldi_loadFromFile ("file");
87-
loadedDldi->ioInterface.startup();
88-
fatMount(&loadedDldi->ioInterface, "devname", 0);
89-
*/
90-
extern DLDI_INTERFACE* dldiLoadFromFile (const char* path);
91-
92-
/*
93-
Free resources used by a loaded DLDI.
94-
Remember to unmount and shutdown first:
95-
fatUnmount("devname");
96-
loadedDldi->ioInterface.shutdown();
97-
dldiFree(loadedDldi);
98-
*/
99-
extern void dldiFree (DLDI_INTERFACE* dldi);
31+
#include "../dldi.h"
10032

101-
#ifdef __cplusplus
102-
}
103-
#endif
10433
#endif // NDS_DLDI_INCLUDE

include/nds/dldi.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
dldi.h
3+
4+
Copyright (c) 2006 Michael "Chishm" Chisholm
5+
6+
Redistribution and use in source and binary forms, with or without modification,
7+
are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice,
10+
this list of conditions and the following disclaimer.
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation and/or
13+
other materials provided with the distribution.
14+
3. The name of the author may not be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19+
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
20+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#pragma once
29+
30+
#ifdef __cplusplus
31+
extern "C" {
32+
#endif
33+
34+
#include <calico/dev/disc_io.h>
35+
#include <calico/dev/dldi.h>
36+
37+
typedef DldiHeader DLDI_INTERFACE;
38+
39+
/// Determine if a given DLDI driver structure is valid
40+
bool dldiIsValid(const DLDI_INTERFACE* io);
41+
42+
/// Find a DLDI driver area within a given binary
43+
DLDI_INTERFACE* dldiFindDriverArea(void* bin, size_t bin_size);
44+
45+
/// Adjust the pointer addresses within a DLDI driver for the target virtual address
46+
void dldiFixDriverAddresses(DLDI_INTERFACE* io, uptr dldi_vaddr);
47+
48+
/// Patch a DLDI driver into a given area
49+
bool dldiApplyPatch(DLDI_INTERFACE* area, const DLDI_INTERFACE* io);
50+
51+
/// Patch a binary using a given DLDI driver
52+
MK_INLINE bool dldiPatchBinary(void* bin, size_t bin_size, const DLDI_INTERFACE* io)
53+
{
54+
DLDI_INTERFACE* area = dldiFindDriverArea(bin, bin_size);
55+
return area ? dldiApplyPatch(area, io) : false;
56+
}
57+
58+
#ifdef __cplusplus
59+
}
60+
#endif

source/arm9/dldi/dldi.c

Lines changed: 0 additions & 187 deletions
This file was deleted.

0 commit comments

Comments
 (0)