ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/rsrc_patches.cpp
Revision: 1.2
Committed: 2003-04-12T10:14:07Z (21 years, 7 months ago) by gbeauche
Branch: MAIN
Changes since 1.1: +2 -0 lines
Log Message:
Make EmulOp() and check_load_invoc() extern "C" so that we are C++ name
mangling independent for asm_linux.S

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * rsrc_patches.cpp - Resource patches
3     *
4     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include <stdio.h>
22     #include <stdlib.h>
23     #include <string.h>
24    
25     #include "sysdeps.h"
26     #include "rsrc_patches.h"
27     #include "cpu_emulation.h"
28     #include "emul_op.h"
29     #include "xlowmem.h"
30     #include "macos_util.h"
31     #include "rom_patches.h"
32     #include "main.h"
33     #include "audio.h"
34    
35     #define DEBUG 0
36     #include "debug.h"
37    
38    
39     // Sound input driver
40     static const uint8 sound_input_driver[] = { // .AppleSoundInput driver header
41     // Driver header
42     0x4d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43     0x00, 0x24, // Open() offset
44     0x00, 0x28, // Prime() offset
45     0x00, 0x2c, // Control() offset
46     0x00, 0x38, // Status() offset
47     0x00, 0x5e, // Close() offset
48     0x10, 0x2e, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x49, 0x6e, 0x70, 0x75, 0x74, 0x00, // ".AppleSoundInput"
49    
50     // Open()
51     M68K_EMUL_OP_SOUNDIN_OPEN >> 8, M68K_EMUL_OP_SOUNDIN_OPEN & 0xff,
52     0x4e, 0x75, // rts
53    
54     // Prime()
55     M68K_EMUL_OP_SOUNDIN_PRIME >> 8, M68K_EMUL_OP_SOUNDIN_PRIME & 0xff,
56     0x60, 0x0e, // bra IOReturn
57    
58     // Control()
59     M68K_EMUL_OP_SOUNDIN_CONTROL >> 8, M68K_EMUL_OP_SOUNDIN_CONTROL & 0xff,
60     0x0c, 0x68, 0x00, 0x01, 0x00, 0x1a, // cmp.w #1,$1a(a0)
61     0x66, 0x04, // bne IOReturn
62     0x4e, 0x75, // rts
63    
64     // Status()
65     M68K_EMUL_OP_SOUNDIN_STATUS >> 8, M68K_EMUL_OP_SOUNDIN_STATUS & 0xff,
66    
67     // IOReturn
68     0x32, 0x28, 0x00, 0x06, // move.w 6(a0),d1
69     0x08, 0x01, 0x00, 0x09, // btst #9,d1
70     0x67, 0x0c, // beq 1
71     0x4a, 0x40, // tst.w d0
72     0x6f, 0x02, // ble 2
73     0x42, 0x40, // clr.w d0
74     0x31, 0x40, 0x00, 0x10, //2 move.w d0,$10(a0)
75     0x4e, 0x75, // rts
76     0x4a, 0x40, //1 tst.w d0
77     0x6f, 0x04, // ble 3
78     0x42, 0x40, // clr.w d0
79     0x4e, 0x75, // rts
80     0x2f, 0x38, 0x08, 0xfc, //3 move.l $8fc,-(sp)
81     0x4e, 0x75, // rts
82    
83     // Close()
84     M68K_EMUL_OP_SOUNDIN_CLOSE >> 8, M68K_EMUL_OP_SOUNDIN_CLOSE & 0xff,
85     0x4e, 0x75, // rts
86     };
87    
88    
89     /*
90     * Search resource for byte string, return offset (or 0)
91     */
92    
93     static uint32 find_rsrc_data(const uint8 *rsrc, uint32 max, const uint8 *search, uint32 search_len, uint32 ofs = 0)
94     {
95     while (ofs < max - search_len) {
96     if (!memcmp(rsrc + ofs, search, search_len))
97     return ofs;
98     ofs++;
99     }
100     return 0;
101     }
102    
103    
104     /*
105     * Resource patches via vCheckLoad
106     */
107    
108     void CheckLoad(uint32 type, int16 id, uint16 *p, uint32 size)
109     {
110     uint16 *p16;
111     uint32 base;
112     D(bug("vCheckLoad %c%c%c%c (%08x) ID %d, data %p, size %d\n", type >> 24, (type >> 16) & 0xff, (type >> 8) & 0xff, type & 0xff, type, id, p, size));
113    
114     // Don't modify resources in ROM
115     if ((uint32)p >= ROM_BASE && (uint32)p <= (ROM_BASE + ROM_SIZE))
116     return;
117    
118     if (type == FOURCC('b','o','o','t') && id == 3) {
119     D(bug("boot 3 found\n"));
120     size >>= 1;
121     while (size--) {
122     if (p[0] == 0x2e49) {
123     // Set boot stack pointer (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1, 8.5, 8.6)
124     p[0] = M68K_EMUL_OP_FIX_BOOTSTACK;
125     D(bug(" patch 1 applied\n"));
126     } else if (p[0] == 0x4267 && p[1] == 0x3f01 && p[2] == 0x3f2a && p[3] == 0x0006 && p[4] == 0x6100) {
127     // Check when ntrb 17 is installed (for native Resource Manager patch) (7.5.3, 7.5.5)
128     p[7] = M68K_EMUL_OP_NTRB_17_PATCH3;
129     D(bug(" patch 2 applied\n"));
130     } else if (p[0] == 0x3f2a && p[1] == 0x0006 && p[2] == 0x3f2a && p[3] == 0x0002 && p[4] == 0x6100) {
131     // Check when ntrb 17 is installed (for native Resource Manager patch) (7.6, 7.6.1, 8.0, 8.1)
132     p[7] = M68K_EMUL_OP_NTRB_17_PATCH;
133     D(bug(" patch 3 applied\n"));
134     } else if (p[0] == 0x3f2a && p[1] == 0x0006 && p[2] == 0x3f2a && p[3] == 0x0002 && p[4] == 0x61ff) {
135     // Check when ntrb 17 is installed (for native Resource Manager patch) (8.5, 8.6)
136     p[8] = M68K_EMUL_OP_NTRB_17_PATCH;
137     D(bug(" patch 4 applied\n"));
138     } else if (p[0] == 0x0c39 && p[1] == 0x0001 && p[2] == 0xf800 && p[3] == 0x0008 && p[4] == 0x6f00) {
139     // Don't read from 0xf8000008 (8.5 with Zanzibar ROM, 8.6)
140     p[0] = M68K_NOP;
141     p[1] = M68K_NOP;
142     p[2] = M68K_NOP;
143     p[3] = M68K_NOP;
144     p[4] = 0x6000; // bra
145     D(bug(" patch 5 applied\n"));
146     } else if (p[0] == 0x2f3c && p[1] == 0x6b72 && p[2] == 0x6e6c && p[3] == 0x4267 && p[4] == 0xa9a0 && p[5] == 0x265f && p[6] == 0x200b && p[7] == 0x6700) {
147     // Don't replace nanokernel ("krnl" resource) (8.6)
148     p[0] = M68K_NOP;
149     p[1] = M68K_NOP;
150     p[2] = M68K_NOP;
151     p[3] = M68K_NOP;
152     p[4] = M68K_NOP;
153     p[7] = 0x6000; // bra
154     D(bug(" patch 6 applied\n"));
155     } else if (p[0] == 0xa8fe && p[1] == 0x3038 && p[2] == 0x017a && p[3] == 0x0c40 && p[4] == 0x8805 && p[5] == 0x6710) {
156     // No SCSI (calls via 0x205c jump vector which is not initialized in NewWorld ROM 1.6) (8.6)
157     if (ROMType == ROMTYPE_NEWWORLD) {
158     p[5] = 0x6010; // bra
159     D(bug(" patch 7 applied\n"));
160     }
161     }
162     p++;
163     }
164    
165     } else if (type == FOURCC('g','n','l','d') && id == 0) {
166     D(bug("gnld 0 found\n"));
167    
168     // Patch native Resource Manager after ntrbs are installed (7.5.2)
169     static const uint8 dat[] = {0x4e, 0xba, 0x00, 0x9e, 0x3e, 0x00, 0x50, 0x4f, 0x67, 0x04};
170     base = find_rsrc_data((uint8 *)p, size, dat, sizeof(dat));
171     if (base) {
172     p16 = (uint16 *)((uint32)p + base + 6);
173     *p16 = htons(M68K_EMUL_OP_NTRB_17_PATCH2);
174     D(bug(" patch 1 applied\n"));
175     }
176    
177     } else if (type == FOURCC('p','t','c','h') && id == 420) {
178     D(bug("ptch 420 found\n"));
179     size >>= 1;
180     while (size--) {
181     if (p[0] == 0xa030 && p[1] == 0x5240 && p[2] == 0x303c && p[3] == 0x0100 && p[4] == 0xc06e && p[5] == 0xfef6) {
182     // Disable VM (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1)
183     p[1] = M68K_NOP;
184     p[2] = M68K_NOP;
185     p[3] = M68K_NOP;
186     p[4] = M68K_NOP;
187     p[5] = M68K_NOP;
188     p[6] = M68K_NOP;
189     p[7] = M68K_NOP;
190     p[8] = M68K_NOP;
191     p[9] = M68K_NOP;
192     p[10] = M68K_NOP;
193     p[11] = M68K_NOP;
194     D(bug(" patch 1 applied\n"));
195     break;
196     } else if (p[0] == 0xa030 && p[1] == 0x5240 && p[2] == 0x7000 && p[3] == 0x302e && p[4] == 0xfef6 && p[5] == 0x323c && p[6] == 0x0100) {
197     // Disable VM (8.0, 8.1)
198     p[8] = M68K_NOP;
199     p[15] = M68K_NOP;
200     D(bug(" patch 2 applied\n"));
201     break;
202     } else if (p[0] == 0xa030 && p[1] == 0x5240 && p[2] == 0x7000 && p[3] == 0x302e && p[4] == 0xfecc && p[5] == 0x323c && p[6] == 0x0100) {
203     // Disable VM (8.5, 8.6)
204     p[8] = M68K_NOP;
205     p[15] = M68K_NOP;
206     D(bug(" patch 3 applied\n"));
207     break;
208     }
209     p++;
210     }
211    
212     } else if (type == FOURCC('g','p','c','h') && id == 16) {
213     D(bug("gpch 16 found\n"));
214     size >>= 1;
215     while (size--) {
216     if (p[0] == 0x6700 && p[13] == 0x7013 && p[14] == 0xfe0a) {
217     // Don't call FE0A in Shutdown Manager (7.6.1, 8.0, 8.1, 8.5)
218     p[0] = 0x6000;
219     D(bug(" patch 1 applied\n"));
220     break;
221     }
222     p++;
223     }
224    
225     } else if (type == FOURCC('g','p','c','h') && id == 650) {
226     D(bug("gpch 650 found\n"));
227     size >>= 1;
228     while (size--) {
229     if (p[0] == 0x6600 && p[1] == 0x001a && p[2] == 0x2278 && p[3] == 0x0134) {
230     // We don't have SonyVars (7.5.2)
231     p[0] = 0x6000;
232     D(bug(" patch 1 applied\n"));
233     } else if (p[0] == 0x6618 && p[1] == 0x2278 && p[2] == 0x0134) {
234     // We don't have SonyVars (7.5.3)
235     p[-6] = M68K_NOP;
236     p[-3] = M68K_NOP;
237     p[0] = 0x6018;
238     D(bug(" patch 2 applied\n"));
239     } else if (p[0] == 0x666e && p[1] == 0x2278 && p[2] == 0x0134) {
240     // We don't have SonyVars (7.5.5)
241     p[-6] = M68K_NOP;
242     p[-3] = M68K_NOP;
243     p[0] = 0x606e;
244     D(bug(" patch 3 applied\n"));
245     } else if (p[0] == 0x6400 && p[1] == 0x011c && p[2] == 0x2278 && p[3] == 0x0134) {
246     // We don't have SonyVars (7.6.1, 8.0, 8.1, 8.5, 8.6)
247     p[0] = 0x6000;
248     D(bug(" patch 4 applied\n"));
249     } else if (p[0] == 0x6400 && p[1] == 0x00e6 && p[2] == 0x2278 && p[3] == 0x0134) {
250     // We don't have SonyVars (7.6)
251     p[0] = 0x6000;
252     D(bug(" patch 5 applied\n"));
253     }
254     p++;
255     }
256    
257     } else if (type == FOURCC('g','p','c','h') && id == 655) {
258     D(bug("gpch 655 found\n"));
259     size >>= 1;
260     while (size--) {
261     if (p[0] == 0x83a8 && p[1] == 0x0024 && p[2] == 0x4e71) {
262     // Don't write to GC interrupt mask (7.6, 7.6.1, 8.0, 8.1 with Zanzibar ROM)
263     p[0] = M68K_NOP;
264     p[1] = M68K_NOP;
265     D(bug(" patch 1 applied\n"));
266     } else if (p[0] == 0x207c && p[1] == 0xf300 && p[2] == 0x0034) {
267     // Don't read PowerMac ID (7.6, 7.6.1, 8.0, 8.1 with Zanzibar ROM)
268     p[0] = 0x303c; // move.w #id,d0
269     p[1] = 0x3020;
270     p[2] = M68K_RTS;
271     D(bug(" patch 2 applied\n"));
272     } else if (p[0] == 0x13fc && p[1] == 0x0081 && p[2] == 0xf130 && p[3] == 0xa030) {
273     // Don't write to hardware (7.6, 7.6.1, 8.0, 8.1 with Zanzibar ROM)
274     p[0] = M68K_NOP;
275     p[1] = M68K_NOP;
276     p[2] = M68K_NOP;
277     p[3] = M68K_NOP;
278     D(bug(" patch 3 applied\n"));
279     } else if (p[0] == 0x4e56 && p[1] == 0x0000 && p[2] == 0x227c && p[3] == 0xf800 && p[4] == 0x0000) {
280     // OpenFirmare? (7.6.1, 8.0, 8.1 with Zanzibar ROM)
281     p[0] = M68K_RTS;
282     D(bug(" patch 4 applied\n"));
283     } else if (p[0] == 0x4e56 && p[1] == 0xfffc && p[2] == 0x48e7 && p[3] == 0x0300 && p[4] == 0x598f && p[5] == 0x2eb8 && p[6] == 0x01dc) {
284     // Don't write to SCC (7.6.1, 8.0, 8.1 with Zanzibar ROM)
285     p[0] = M68K_RTS;
286     D(bug(" patch 5 applied\n"));
287     } else if (p[0] == 0x4e56 && p[1] == 0x0000 && p[2] == 0x227c && p[3] == 0xf300 && p[4] == 0x0034) {
288     // Don't write to GC (7.6.1, 8.0, 8.1 with Zanzibar ROM)
289     p[0] = M68K_RTS;
290     D(bug(" patch 6 applied\n"));
291     } else if (p[0] == 0x40e7 && p[1] == 0x007c && p[2] == 0x0700 && p[3] == 0x48e7 && p[4] == 0x00c0 && p[5] == 0x2078 && p[6] == 0x0dd8 && p[7] == 0xd1e8 && p[8] == 0x0044 && p[9] == 0x8005 && p[11] == 0x93c8 && p[12] == 0x2149 && p[13] == 0x0024) {
292     // Don't replace NVRAM routines (7.6, 7.6.1, 8.0, 8.1 with Zanzibar ROM)
293     p[0] = M68K_RTS;
294     D(bug(" patch 7 applied\n"));
295     } else if (p[0] == 0x207c && p[1] == 0x50f1 && p[2] == 0xa101 && (p[3] == 0x08d0 || p[3] == 0x0890)) {
296     // Don't write to 0x50f1a101 (8.1 with Zanzibar ROM)
297     p[3] = M68K_NOP;
298     p[4] = M68K_NOP;
299     D(bug(" patch 8 applied\n"));
300     }
301     p++;
302     }
303    
304     } else if (type == FOURCC('g','p','c','h') && id == 750) {
305     D(bug("gpch 750 found\n"));
306     size >>= 1;
307     while (size--) {
308     if (p[0] == 0xf301 && p[1] == 0x9100 && p[2] == 0x0c11 && p[3] == 0x0044) {
309     // Don't read from 0xf3019100 (MACE ENET) (7.6, 7.6.1, 8.0, 8.1)
310     p[2] = M68K_NOP;
311     p[3] = M68K_NOP;
312     p[4] = 0x6026;
313     D(bug(" patch 1 applied\n"));
314     } else if (p[0] == 0x41e8 && p[1] == 0x0374 && p[2] == 0xfc1e) {
315     // Don't call FC1E opcode (7.6, 7.6.1, 8.0, 8.1, 8.5, 8.6)
316     p[2] = M68K_NOP;
317     D(bug(" patch 2 applied\n"));
318     } else if (p[0] == 0x700a && p[1] == 0xfe0a) {
319     // Don't call FE0A opcode (7.6, 7.6.1, 8.0, 8.1, 8.5, 8.6)
320     p[1] = 0x7000;
321     D(bug(" patch 3 applied\n"));
322     }
323     p++;
324     }
325    
326     } else if (type == FOURCC('g','p','c','h') && id == 999) {
327     D(bug("gpch 999 found\n"));
328     size >>= 1;
329     while (size--) {
330     if (p[0] == 0xf301 && p[1] == 0x9100 && p[2] == 0x0c11 && p[3] == 0x0044) {
331     // Don't read from 0xf3019100 (MACE ENET) (8.5, 8.6)
332     p[2] = M68K_NOP;
333     p[3] = M68K_NOP;
334     p[4] = 0x6026;
335     D(bug(" patch 1 applied\n"));
336     }
337     p++;
338     }
339    
340     } else if (type == FOURCC('g','p','c','h') && id == 3000) {
341     D(bug("gpch 3000 found\n"));
342     size >>= 1;
343     while (size--) {
344     if (p[0] == 0xf301 && p[1] == 0x9100 && p[2] == 0x0c11 && p[3] == 0x0044) {
345     // Don't read from 0xf3019100 (MACE ENET) (8.1 with NewWorld ROM)
346     p[2] = M68K_NOP;
347     p[3] = M68K_NOP;
348     p[4] = 0x6026;
349     D(bug(" patch 1 applied\n"));
350     }
351     p++;
352     }
353    
354     } else if (type == FOURCC('l','t','l','k') && id == 0) {
355     D(bug("ltlk 0 found\n"));
356     #if 1
357     size >>= 1;
358     while (size--) {
359     if (p[0] == 0xc2fc && p[1] == 0x0fa0 && p[2] == 0x82c5) {
360     // Prevent division by 0 in speed test (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
361     p[2] = 0x7200;
362     WriteMacInt32(0x1d8, 0x2c00);
363     WriteMacInt32(0x1dc, 0x2c00);
364     D(bug(" patch 1 applied\n"));
365     } else if (p[0] == 0x1418 && p[1] == 0x84c1) {
366     // Prevent division by 0 (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
367     p[1] = 0x7400;
368     D(bug(" patch 2 applied\n"));
369     } else if (p[0] == 0x2678 && p[1] == 0x01dc && p[2] == 0x3018 && p[3] == 0x6708 && p[4] == 0x1680 && p[5] == 0xe058 && p[6] == 0x1680) {
370     // Don't write to SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
371     p[4] = M68K_NOP;
372     p[6] = M68K_NOP;
373     D(bug(" patch 3 applied\n"));
374     } else if (p[0] == 0x2278 && p[1] == 0x01dc && p[2] == 0x12bc && p[3] == 0x0006 && p[4] == 0x4e71 && p[5] == 0x1292) {
375     // Don't write to SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
376     p[2] = M68K_NOP;
377     p[3] = M68K_NOP;
378     p[5] = M68K_NOP;
379     D(bug(" patch 4 applied\n"));
380     } else if (p[0] == 0x2278 && p[1] == 0x01dc && p[2] == 0x12bc && p[3] == 0x0003 && p[4] == 0x4e71 && p[5] == 0x1281) {
381     // Don't write to SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
382     p[2] = M68K_NOP;
383     p[3] = M68K_NOP;
384     p[5] = M68K_NOP;
385     D(bug(" patch 5 applied\n"));
386     } else if (p[0] == 0x0811 && p[1] == 0x0000 && p[2] == 0x51c8 && p[3] == 0xfffa) {
387     // Don't test SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
388     p[0] = M68K_NOP;
389     p[1] = M68K_NOP;
390     D(bug(" patch 6 applied\n"));
391     } else if (p[0] == 0x4a2a && p[1] == 0x063e && p[2] == 0x66fa) {
392     // Don't wait for SCC (7.5.2, 7.5.3, 7.5.5)
393     p[2] = M68K_NOP;
394     D(bug(" patch 7 applied\n"));
395     } else if (p[0] == 0x4a2a && p[1] == 0x03a6 && p[2] == 0x66fa) {
396     // Don't wait for SCC (7.6, 7.6.1, 8.0, 8.1)
397     p[2] = M68K_NOP;
398     D(bug(" patch 8 applied\n"));
399     }
400     p++;
401     }
402     #else
403     // Disable LocalTalk
404     p[0] = M68K_JMP_A0;
405     p[1] = 0x7000; // moveq #0,d0
406     p[2] = M68K_RTS;
407     D(bug(" patch 1 applied\n"));
408     #endif
409    
410     } else if (type == FOURCC('n','s','r','d') && id == 1) {
411     D(bug("nsrd 1 found\n"));
412     if (p[(0x378 + 0x570) >> 1] == 0x7c08 && p[(0x37a + 0x570) >> 1] == 0x02a6) {
413     // Don't overwrite our serial drivers (8.0, 8.1)
414     p[(0x378 + 0x570) >> 1] = 0x4e80; // blr
415     p[(0x37a + 0x570) >> 1] = 0x0020;
416     D(bug(" patch 1 applied\n"));
417     } else if (p[(0x378 + 0x6c0) >> 1] == 0x7c08 && p[(0x37a + 0x6c0) >> 1] == 0x02a6) {
418     // Don't overwrite our serial drivers (8.5, 8.6)
419     p[(0x378 + 0x6c0) >> 1] = 0x4e80; // blr
420     p[(0x37a + 0x6c0) >> 1] = 0x0020;
421     D(bug(" patch 2 applied\n"));
422     }
423    
424     } else if (type == FOURCC('c','i','t','t') && id == 45) {
425     D(bug("citt 45 found\n"));
426     size >>= 1;
427     while (size--) {
428     if (p[0] == 0x203c && p[1] == 0x0100 && p[2] == 0x0000 && p[3] == 0xc0ae && p[4] == 0xfffc) {
429     // Don't replace SCSI Manager (8.1, 8.5, 8.6)
430     p[5] = (p[5] & 0xff) | 0x6000; // beq
431     D(bug(" patch 1 applied\n"));
432     break;
433     }
434     p++;
435     }
436    
437     } else if (type == FOURCC('t','h','n','g')) {
438     // Collect info about used audio sifters
439     uint32 c_type = 0[(uint32 *)p];
440     uint32 sub_type = 1[(uint32 *)p];
441     if (c_type == FOURCC('s','d','e','v') && sub_type == FOURCC('s','i','n','g')) {
442     1[(uint32 *)p] = FOURCC('a','w','g','c');
443     D(bug("thng %d, type %c%c%c%c (%08x), sub type %c%c%c%c (%08x), data %p\n", id, c_type >> 24, (c_type >> 16) & 0xff, (c_type >> 8) & 0xff, c_type & 0xff, c_type, sub_type >> 24, (sub_type >> 16) & 0xff, (sub_type >> 8) & 0xff, sub_type & 0xff, sub_type, p));
444     AddSifter(*(uint32 *)(((uint32)p)+20), p[12]);
445     if (p[28]) // componentPFCount
446     AddSifter(*(uint32 *)(((uint32)p)+62), p[33]);
447     }
448    
449     } else if (type == FOURCC('s','i','f','t') || type == FOURCC('n','i','f','t')) {
450     // Patch audio sifters
451     if (FindSifter(type, id)) {
452     D(bug("sifter found\n"));
453     p[0] = 0x4e56; p[1] = 0x0000; // link a6,#0
454     p[2] = 0x48e7; p[3] = 0x8018; // movem.l d0/a3-a4,-(a7)
455     p[4] = 0x266e; p[5] = 0x000c; // movea.l $c(a6),a3
456     p[6] = 0x286e; p[7] = 0x0008; // movea.l $8(a6),a4
457     p[8] = M68K_EMUL_OP_AUDIO_DISPATCH;
458     p[9] = 0x2d40; p[10] = 0x0010; // move.l d0,$10(a6)
459     p[11] = 0x4cdf; p[12] = 0x1801; // movem.l (a7)+,d0/a3-a4
460     p[13] = 0x4e5e; // unlk a6
461     p[14] = 0x4e74; p[15] = 0x0008; // rtd #8
462     D(bug(" patch applied\n"));
463     }
464    
465     } else if (type == FOURCC('D','R','V','R') && (id == -16501 || id == -16500)) {
466     D(bug("DRVR -16501/-16500 found\n"));
467     // Install sound input driver
468     memcpy(p, sound_input_driver, sizeof(sound_input_driver));
469     D(bug(" patch 1 applied\n"));
470    
471     } else if (type == FOURCC('I','N','I','T') && id == 1 && size == (2416 >> 1)) {
472     D(bug("INIT 1 (size 2416) found\n"));
473     size >>= 1;
474     while (size--) {
475     if (p[0] == 0x247c && p[1] == 0xf301 && p[2] == 0x9000) {
476     // Prevent "MacOS Licensing Extension" from accessing hardware (7.6)
477     p[22] = 0x6028;
478     D(bug(" patch 1 applied\n"));
479     break;
480     }
481     p++;
482     }
483     }
484     }
485    
486    
487     /*
488     * Native Resource Manager patches
489     */
490    
491     #ifdef __BEOS__
492     static
493 gbeauche 1.2 #else
494     extern "C"
495 cebix 1.1 #endif
496     void check_load_invoc(uint32 type, int16 id, uint16 **h)
497     {
498     if (h == NULL)
499     return;
500     uint16 *p = *h;
501     if (p == NULL)
502     return;
503     uint32 size = ((uint32 *)p)[-2] & 0xffffff;
504    
505     CheckLoad(type, id, p, size);
506     }
507    
508     #ifdef __BEOS__
509     static asm void **get_resource(register uint32 type, register int16 id)
510     {
511     // Create stack frame
512     mflr r0
513     stw r0,8(r1)
514     stwu r1,-(56+12)(r1)
515    
516     // Save type/ID
517     stw r3,56(r1)
518     stw r4,56+4(r1)
519    
520     // Call old routine
521     lwz r0,XLM_GET_RESOURCE
522     lwz r2,XLM_RES_LIB_TOC
523     mtctr r0
524     bctrl
525     lwz r2,XLM_TOC // Get TOC
526     stw r3,56+8(r1) // Save handle
527    
528     // Call CheckLoad
529     lwz r3,56(r1)
530     lwz r4,56+4(r1)
531     lwz r5,56+8(r1)
532     bl check_load_invoc
533     lwz r3,56+8(r1) // Restore handle
534    
535     // Return to caller
536     lwz r0,56+12+8(r1)
537     mtlr r0
538     addi r1,r1,56+12
539     blr
540     }
541    
542     static asm void **get_1_resource(register uint32 type, register int16 id)
543     {
544     // Create stack frame
545     mflr r0
546     stw r0,8(r1)
547     stwu r1,-(56+12)(r1)
548    
549     // Save type/ID
550     stw r3,56(r1)
551     stw r4,56+4(r1)
552    
553     // Call old routine
554     lwz r0,XLM_GET_1_RESOURCE
555     lwz r2,XLM_RES_LIB_TOC
556     mtctr r0
557     bctrl
558     lwz r2,XLM_TOC // Get TOC
559     stw r3,56+8(r1) // Save handle
560    
561     // Call CheckLoad
562     lwz r3,56(r1)
563     lwz r4,56+4(r1)
564     lwz r5,56+8(r1)
565     bl check_load_invoc
566     lwz r3,56+8(r1) // Restore handle
567    
568     // Return to caller
569     lwz r0,56+12+8(r1)
570     mtlr r0
571     addi r1,r1,56+12
572     blr
573     }
574    
575     static asm void **get_ind_resource(register uint32 type, register int16 index)
576     {
577     // Create stack frame
578     mflr r0
579     stw r0,8(r1)
580     stwu r1,-(56+12)(r1)
581    
582     // Save type/index
583     stw r3,56(r1)
584     stw r4,56+4(r1)
585    
586     // Call old routine
587     lwz r0,XLM_GET_IND_RESOURCE
588     lwz r2,XLM_RES_LIB_TOC
589     mtctr r0
590     bctrl
591     lwz r2,XLM_TOC // Get TOC
592     stw r3,56+8(r1) // Save handle
593    
594     // Call CheckLoad
595     lwz r3,56(r1)
596     lwz r4,56+4(r1)
597     lwz r5,56+8(r1)
598     bl check_load_invoc
599     lwz r3,56+8(r1) // Restore handle
600    
601     // Return to caller
602     lwz r0,56+12+8(r1)
603     mtlr r0
604     addi r1,r1,56+12
605     blr
606     }
607    
608     static asm void **get_1_ind_resource(register uint32 type, register int16 index)
609     {
610     // Create stack frame
611     mflr r0
612     stw r0,8(r1)
613     stwu r1,-(56+12)(r1)
614    
615     // Save type/index
616     stw r3,56(r1)
617     stw r4,56+4(r1)
618    
619     // Call old routine
620     lwz r0,XLM_GET_1_IND_RESOURCE
621     lwz r2,XLM_RES_LIB_TOC
622     mtctr r0
623     bctrl
624     lwz r2,XLM_TOC // Get TOC
625     stw r3,56+8(r1) // Save handle
626    
627     // Call CheckLoad
628     lwz r3,56(r1)
629     lwz r4,56+4(r1)
630     lwz r5,56+8(r1)
631     bl check_load_invoc
632     lwz r3,56+8(r1) // Restore handle
633    
634     // Return to caller
635     lwz r0,56+12+8(r1)
636     mtlr r0
637     addi r1,r1,56+12
638     blr
639     }
640    
641     static asm void **r_get_resource(register uint32 type, register int16 id)
642     {
643     // Create stack frame
644     mflr r0
645     stw r0,8(r1)
646     stwu r1,-(56+12)(r1)
647    
648     // Save type/ID
649     stw r3,56(r1)
650     stw r4,56+4(r1)
651    
652     // Call old routine
653     lwz r0,XLM_R_GET_RESOURCE
654     lwz r2,XLM_RES_LIB_TOC
655     mtctr r0
656     bctrl
657     lwz r2,XLM_TOC // Get TOC
658     stw r3,56+8(r1) // Save handle
659    
660     // Call CheckLoad
661     lwz r3,56(r1)
662     lwz r4,56+4(r1)
663     lwz r5,56+8(r1)
664     bl check_load_invoc
665     lwz r3,56+8(r1) // Restore handle
666    
667     // Return to caller
668     lwz r0,56+12+8(r1)
669     mtlr r0
670     addi r1,r1,56+12
671     blr
672     }
673     #else
674     // Routines in asm_linux.S
675     extern "C" void get_resource(void);
676     extern "C" void get_1_resource(void);
677     extern "C" void get_ind_resource(void);
678     extern "C" void get_1_ind_resource(void);
679     extern "C" void r_get_resource(void);
680     #endif
681    
682     void PatchNativeResourceManager(void)
683     {
684     D(bug("PatchNativeResourceManager\n"));
685    
686     // Patch native GetResource()
687     uint32 **upp = *(uint32 ***)0x1480;
688     if (((uint32)upp & 0xffc00000) == ROM_BASE)
689     return;
690     uint32 *tvec = upp[5];
691     D(bug(" GetResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
692     *(uint32 *)XLM_RES_LIB_TOC = tvec[1];
693     *(uint32 *)XLM_GET_RESOURCE = tvec[0];
694     #ifdef __BEOS__
695     uint32 *tvec2 = (uint32 *)get_resource;
696     tvec[0] = tvec2[0];
697     tvec[1] = tvec2[1];
698     #else
699     tvec[0] = (uint32)get_resource;
700     #endif
701    
702     // Patch native Get1Resource()
703     upp = *(uint32 ***)0xe7c;
704     tvec = upp[5];
705     D(bug(" Get1Resource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
706     *(uint32 *)XLM_GET_1_RESOURCE = tvec[0];
707     #ifdef __BEOS__
708     tvec2 = (uint32 *)get_1_resource;
709     tvec[0] = tvec2[0];
710     tvec[1] = tvec2[1];
711     #else
712     tvec[0] = (uint32)get_1_resource;
713     #endif
714    
715     // Patch native GetIndResource()
716     upp = *(uint32 ***)0x1474;
717     tvec = upp[5];
718     D(bug(" GetIndResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
719     *(uint32 *)XLM_GET_IND_RESOURCE = tvec[0];
720     #ifdef __BEOS__
721     tvec2 = (uint32 *)get_ind_resource;
722     tvec[0] = tvec2[0];
723     tvec[1] = tvec2[1];
724     #else
725     tvec[0] = (uint32)get_ind_resource;
726     #endif
727    
728     // Patch native Get1IndResource()
729     upp = *(uint32 ***)0xe38;
730     tvec = upp[5];
731     D(bug(" Get1IndResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
732     *(uint32 *)XLM_GET_1_IND_RESOURCE = tvec[0];
733     #ifdef __BEOS__
734     tvec2 = (uint32 *)get_1_ind_resource;
735     tvec[0] = tvec2[0];
736     tvec[1] = tvec2[1];
737     #else
738     tvec[0] = (uint32)get_1_ind_resource;
739     #endif
740    
741     // Patch native RGetResource()
742     upp = *(uint32 ***)0xe30;
743     tvec = upp[5];
744     D(bug(" RGetResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
745     *(uint32 *)XLM_R_GET_RESOURCE = tvec[0];
746     #ifdef __BEOS__
747     tvec2 = (uint32 *)r_get_resource;
748     tvec[0] = tvec2[0];
749     tvec[1] = tvec2[1];
750     #else
751     tvec[0] = (uint32)r_get_resource;
752     #endif
753     }