ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/rsrc_patches.cpp
Revision: 1.3
Committed: 2003-09-07T14:33:51Z (20 years, 8 months ago) by gbeauche
Branch: MAIN
Changes since 1.2: +42 -1 lines
Log Message:
- Integrate new NativeOp instructions to be used as trampolines to call
  native functions from ppc code.
- Little endian fixes in emul_op.cpp
- Add new 'gpch' 750 patch to workaround crash with MacOS 8.6
- Don't crash in Process Manager on reset/shutdown with MacOS 8.6
- We also have an experimental interrupt thread in emulation mode

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 gbeauche 1.3 } else if (p[0] == 0x6c00 && p[1] == 0x016a && p[2] == 0x2278 && p[3] == 0x0134) {
323     // We don't have SonyVars (8.6)
324     p[-4] = 0x21fc; // move.l $40810000,($0000)
325     p[-3] = 0x4081;
326     p[-2] = 0x0000;
327     p[-1] = 0x0000;
328     p[0] = 0x6000;
329     D(bug(" patch 4 applied\n"));
330 cebix 1.1 }
331     p++;
332     }
333    
334     } else if (type == FOURCC('g','p','c','h') && id == 999) {
335     D(bug("gpch 999 found\n"));
336     size >>= 1;
337     while (size--) {
338     if (p[0] == 0xf301 && p[1] == 0x9100 && p[2] == 0x0c11 && p[3] == 0x0044) {
339     // Don't read from 0xf3019100 (MACE ENET) (8.5, 8.6)
340     p[2] = M68K_NOP;
341     p[3] = M68K_NOP;
342     p[4] = 0x6026;
343     D(bug(" patch 1 applied\n"));
344     }
345     p++;
346     }
347    
348     } else if (type == FOURCC('g','p','c','h') && id == 3000) {
349     D(bug("gpch 3000 found\n"));
350     size >>= 1;
351     while (size--) {
352     if (p[0] == 0xf301 && p[1] == 0x9100 && p[2] == 0x0c11 && p[3] == 0x0044) {
353     // Don't read from 0xf3019100 (MACE ENET) (8.1 with NewWorld ROM)
354     p[2] = M68K_NOP;
355     p[3] = M68K_NOP;
356     p[4] = 0x6026;
357     D(bug(" patch 1 applied\n"));
358     }
359     p++;
360     }
361    
362     } else if (type == FOURCC('l','t','l','k') && id == 0) {
363     D(bug("ltlk 0 found\n"));
364     #if 1
365     size >>= 1;
366     while (size--) {
367     if (p[0] == 0xc2fc && p[1] == 0x0fa0 && p[2] == 0x82c5) {
368     // 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)
369     p[2] = 0x7200;
370     WriteMacInt32(0x1d8, 0x2c00);
371     WriteMacInt32(0x1dc, 0x2c00);
372     D(bug(" patch 1 applied\n"));
373     } else if (p[0] == 0x1418 && p[1] == 0x84c1) {
374     // Prevent division by 0 (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
375     p[1] = 0x7400;
376     D(bug(" patch 2 applied\n"));
377     } else if (p[0] == 0x2678 && p[1] == 0x01dc && p[2] == 0x3018 && p[3] == 0x6708 && p[4] == 0x1680 && p[5] == 0xe058 && p[6] == 0x1680) {
378     // Don't write to SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
379     p[4] = M68K_NOP;
380     p[6] = M68K_NOP;
381     D(bug(" patch 3 applied\n"));
382     } else if (p[0] == 0x2278 && p[1] == 0x01dc && p[2] == 0x12bc && p[3] == 0x0006 && p[4] == 0x4e71 && p[5] == 0x1292) {
383     // Don't write to SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
384     p[2] = M68K_NOP;
385     p[3] = M68K_NOP;
386     p[5] = M68K_NOP;
387     D(bug(" patch 4 applied\n"));
388     } else if (p[0] == 0x2278 && p[1] == 0x01dc && p[2] == 0x12bc && p[3] == 0x0003 && p[4] == 0x4e71 && p[5] == 0x1281) {
389     // Don't write to SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
390     p[2] = M68K_NOP;
391     p[3] = M68K_NOP;
392     p[5] = M68K_NOP;
393     D(bug(" patch 5 applied\n"));
394     } else if (p[0] == 0x0811 && p[1] == 0x0000 && p[2] == 0x51c8 && p[3] == 0xfffa) {
395     // Don't test SCC (7.5.2, 7.5.3, 7.5.5, 7.6, 7.6.1, 8.0, 8.1)
396     p[0] = M68K_NOP;
397     p[1] = M68K_NOP;
398     D(bug(" patch 6 applied\n"));
399     } else if (p[0] == 0x4a2a && p[1] == 0x063e && p[2] == 0x66fa) {
400     // Don't wait for SCC (7.5.2, 7.5.3, 7.5.5)
401     p[2] = M68K_NOP;
402     D(bug(" patch 7 applied\n"));
403     } else if (p[0] == 0x4a2a && p[1] == 0x03a6 && p[2] == 0x66fa) {
404     // Don't wait for SCC (7.6, 7.6.1, 8.0, 8.1)
405     p[2] = M68K_NOP;
406     D(bug(" patch 8 applied\n"));
407     }
408     p++;
409     }
410     #else
411     // Disable LocalTalk
412     p[0] = M68K_JMP_A0;
413     p[1] = 0x7000; // moveq #0,d0
414     p[2] = M68K_RTS;
415     D(bug(" patch 1 applied\n"));
416     #endif
417    
418     } else if (type == FOURCC('n','s','r','d') && id == 1) {
419     D(bug("nsrd 1 found\n"));
420     if (p[(0x378 + 0x570) >> 1] == 0x7c08 && p[(0x37a + 0x570) >> 1] == 0x02a6) {
421     // Don't overwrite our serial drivers (8.0, 8.1)
422     p[(0x378 + 0x570) >> 1] = 0x4e80; // blr
423     p[(0x37a + 0x570) >> 1] = 0x0020;
424     D(bug(" patch 1 applied\n"));
425     } else if (p[(0x378 + 0x6c0) >> 1] == 0x7c08 && p[(0x37a + 0x6c0) >> 1] == 0x02a6) {
426     // Don't overwrite our serial drivers (8.5, 8.6)
427     p[(0x378 + 0x6c0) >> 1] = 0x4e80; // blr
428     p[(0x37a + 0x6c0) >> 1] = 0x0020;
429     D(bug(" patch 2 applied\n"));
430     }
431    
432     } else if (type == FOURCC('c','i','t','t') && id == 45) {
433     D(bug("citt 45 found\n"));
434     size >>= 1;
435     while (size--) {
436     if (p[0] == 0x203c && p[1] == 0x0100 && p[2] == 0x0000 && p[3] == 0xc0ae && p[4] == 0xfffc) {
437     // Don't replace SCSI Manager (8.1, 8.5, 8.6)
438     p[5] = (p[5] & 0xff) | 0x6000; // beq
439     D(bug(" patch 1 applied\n"));
440     break;
441     }
442     p++;
443     }
444    
445     } else if (type == FOURCC('t','h','n','g')) {
446     // Collect info about used audio sifters
447     uint32 c_type = 0[(uint32 *)p];
448     uint32 sub_type = 1[(uint32 *)p];
449     if (c_type == FOURCC('s','d','e','v') && sub_type == FOURCC('s','i','n','g')) {
450     1[(uint32 *)p] = FOURCC('a','w','g','c');
451     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));
452     AddSifter(*(uint32 *)(((uint32)p)+20), p[12]);
453     if (p[28]) // componentPFCount
454     AddSifter(*(uint32 *)(((uint32)p)+62), p[33]);
455     }
456    
457     } else if (type == FOURCC('s','i','f','t') || type == FOURCC('n','i','f','t')) {
458     // Patch audio sifters
459     if (FindSifter(type, id)) {
460     D(bug("sifter found\n"));
461     p[0] = 0x4e56; p[1] = 0x0000; // link a6,#0
462     p[2] = 0x48e7; p[3] = 0x8018; // movem.l d0/a3-a4,-(a7)
463     p[4] = 0x266e; p[5] = 0x000c; // movea.l $c(a6),a3
464     p[6] = 0x286e; p[7] = 0x0008; // movea.l $8(a6),a4
465     p[8] = M68K_EMUL_OP_AUDIO_DISPATCH;
466     p[9] = 0x2d40; p[10] = 0x0010; // move.l d0,$10(a6)
467     p[11] = 0x4cdf; p[12] = 0x1801; // movem.l (a7)+,d0/a3-a4
468     p[13] = 0x4e5e; // unlk a6
469     p[14] = 0x4e74; p[15] = 0x0008; // rtd #8
470     D(bug(" patch applied\n"));
471     }
472    
473     } else if (type == FOURCC('D','R','V','R') && (id == -16501 || id == -16500)) {
474     D(bug("DRVR -16501/-16500 found\n"));
475     // Install sound input driver
476     memcpy(p, sound_input_driver, sizeof(sound_input_driver));
477     D(bug(" patch 1 applied\n"));
478    
479     } else if (type == FOURCC('I','N','I','T') && id == 1 && size == (2416 >> 1)) {
480     D(bug("INIT 1 (size 2416) found\n"));
481     size >>= 1;
482     while (size--) {
483     if (p[0] == 0x247c && p[1] == 0xf301 && p[2] == 0x9000) {
484     // Prevent "MacOS Licensing Extension" from accessing hardware (7.6)
485     p[22] = 0x6028;
486     D(bug(" patch 1 applied\n"));
487     break;
488     }
489     p++;
490     }
491 gbeauche 1.3
492     } else if (type == FOURCC('s','c','o','d') && id == -16465) {
493     D(bug("scod -16465 found\n"));
494    
495     // Don't crash in Process Manager on reset/shutdown (8.6)
496     static const uint8 dat[] = {0x4e, 0x56, 0x00, 0x00, 0x48, 0xe7, 0x03, 0x18, 0x2c, 0x2e, 0x00, 0x10};
497     base = find_rsrc_data((uint8 *)p, size, dat, sizeof(dat));
498     if (base) {
499     p16 = (uint16 *)((uint32)p + base);
500     p16[0] = 0x7000; // moveq #0,d0
501     p16[1] = M68K_RTS;
502     D(bug(" patch 1 applied\n"));
503     }
504 cebix 1.1 }
505     }
506    
507    
508     /*
509     * Native Resource Manager patches
510     */
511    
512     #ifdef __BEOS__
513     static
514 gbeauche 1.2 #else
515     extern "C"
516 cebix 1.1 #endif
517     void check_load_invoc(uint32 type, int16 id, uint16 **h)
518     {
519     if (h == NULL)
520     return;
521     uint16 *p = *h;
522     if (p == NULL)
523     return;
524     uint32 size = ((uint32 *)p)[-2] & 0xffffff;
525    
526     CheckLoad(type, id, p, size);
527     }
528    
529     #ifdef __BEOS__
530     static asm void **get_resource(register uint32 type, register int16 id)
531     {
532     // Create stack frame
533     mflr r0
534     stw r0,8(r1)
535     stwu r1,-(56+12)(r1)
536    
537     // Save type/ID
538     stw r3,56(r1)
539     stw r4,56+4(r1)
540    
541     // Call old routine
542     lwz r0,XLM_GET_RESOURCE
543     lwz r2,XLM_RES_LIB_TOC
544     mtctr r0
545     bctrl
546     lwz r2,XLM_TOC // Get TOC
547     stw r3,56+8(r1) // Save handle
548    
549     // Call CheckLoad
550     lwz r3,56(r1)
551     lwz r4,56+4(r1)
552     lwz r5,56+8(r1)
553     bl check_load_invoc
554     lwz r3,56+8(r1) // Restore handle
555    
556     // Return to caller
557     lwz r0,56+12+8(r1)
558     mtlr r0
559     addi r1,r1,56+12
560     blr
561     }
562    
563     static asm void **get_1_resource(register uint32 type, register int16 id)
564     {
565     // Create stack frame
566     mflr r0
567     stw r0,8(r1)
568     stwu r1,-(56+12)(r1)
569    
570     // Save type/ID
571     stw r3,56(r1)
572     stw r4,56+4(r1)
573    
574     // Call old routine
575     lwz r0,XLM_GET_1_RESOURCE
576     lwz r2,XLM_RES_LIB_TOC
577     mtctr r0
578     bctrl
579     lwz r2,XLM_TOC // Get TOC
580     stw r3,56+8(r1) // Save handle
581    
582     // Call CheckLoad
583     lwz r3,56(r1)
584     lwz r4,56+4(r1)
585     lwz r5,56+8(r1)
586     bl check_load_invoc
587     lwz r3,56+8(r1) // Restore handle
588    
589     // Return to caller
590     lwz r0,56+12+8(r1)
591     mtlr r0
592     addi r1,r1,56+12
593     blr
594     }
595    
596     static asm void **get_ind_resource(register uint32 type, register int16 index)
597     {
598     // Create stack frame
599     mflr r0
600     stw r0,8(r1)
601     stwu r1,-(56+12)(r1)
602    
603     // Save type/index
604     stw r3,56(r1)
605     stw r4,56+4(r1)
606    
607     // Call old routine
608     lwz r0,XLM_GET_IND_RESOURCE
609     lwz r2,XLM_RES_LIB_TOC
610     mtctr r0
611     bctrl
612     lwz r2,XLM_TOC // Get TOC
613     stw r3,56+8(r1) // Save handle
614    
615     // Call CheckLoad
616     lwz r3,56(r1)
617     lwz r4,56+4(r1)
618     lwz r5,56+8(r1)
619     bl check_load_invoc
620     lwz r3,56+8(r1) // Restore handle
621    
622     // Return to caller
623     lwz r0,56+12+8(r1)
624     mtlr r0
625     addi r1,r1,56+12
626     blr
627     }
628    
629     static asm void **get_1_ind_resource(register uint32 type, register int16 index)
630     {
631     // Create stack frame
632     mflr r0
633     stw r0,8(r1)
634     stwu r1,-(56+12)(r1)
635    
636     // Save type/index
637     stw r3,56(r1)
638     stw r4,56+4(r1)
639    
640     // Call old routine
641     lwz r0,XLM_GET_1_IND_RESOURCE
642     lwz r2,XLM_RES_LIB_TOC
643     mtctr r0
644     bctrl
645     lwz r2,XLM_TOC // Get TOC
646     stw r3,56+8(r1) // Save handle
647    
648     // Call CheckLoad
649     lwz r3,56(r1)
650     lwz r4,56+4(r1)
651     lwz r5,56+8(r1)
652     bl check_load_invoc
653     lwz r3,56+8(r1) // Restore handle
654    
655     // Return to caller
656     lwz r0,56+12+8(r1)
657     mtlr r0
658     addi r1,r1,56+12
659     blr
660     }
661    
662     static asm void **r_get_resource(register uint32 type, register int16 id)
663     {
664     // Create stack frame
665     mflr r0
666     stw r0,8(r1)
667     stwu r1,-(56+12)(r1)
668    
669     // Save type/ID
670     stw r3,56(r1)
671     stw r4,56+4(r1)
672    
673     // Call old routine
674     lwz r0,XLM_R_GET_RESOURCE
675     lwz r2,XLM_RES_LIB_TOC
676     mtctr r0
677     bctrl
678     lwz r2,XLM_TOC // Get TOC
679     stw r3,56+8(r1) // Save handle
680    
681     // Call CheckLoad
682     lwz r3,56(r1)
683     lwz r4,56+4(r1)
684     lwz r5,56+8(r1)
685     bl check_load_invoc
686     lwz r3,56+8(r1) // Restore handle
687    
688     // Return to caller
689     lwz r0,56+12+8(r1)
690     mtlr r0
691     addi r1,r1,56+12
692     blr
693     }
694     #else
695     // Routines in asm_linux.S
696     extern "C" void get_resource(void);
697     extern "C" void get_1_resource(void);
698     extern "C" void get_ind_resource(void);
699     extern "C" void get_1_ind_resource(void);
700     extern "C" void r_get_resource(void);
701     #endif
702    
703     void PatchNativeResourceManager(void)
704     {
705     D(bug("PatchNativeResourceManager\n"));
706    
707     // Patch native GetResource()
708 gbeauche 1.3 uint32 **upp = (uint32 **)(uintptr)ReadMacInt32(0x1480);
709 cebix 1.1 if (((uint32)upp & 0xffc00000) == ROM_BASE)
710     return;
711     uint32 *tvec = upp[5];
712     D(bug(" GetResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
713     *(uint32 *)XLM_RES_LIB_TOC = tvec[1];
714     *(uint32 *)XLM_GET_RESOURCE = tvec[0];
715 gbeauche 1.3 #if EMULATED_PPC
716     tvec[0] = POWERPC_NATIVE_OP_FUNC(NATIVE_GET_RESOURCE);
717     #else
718 cebix 1.1 #ifdef __BEOS__
719     uint32 *tvec2 = (uint32 *)get_resource;
720     tvec[0] = tvec2[0];
721     tvec[1] = tvec2[1];
722     #else
723     tvec[0] = (uint32)get_resource;
724     #endif
725 gbeauche 1.3 #endif
726 cebix 1.1
727     // Patch native Get1Resource()
728     upp = *(uint32 ***)0xe7c;
729     tvec = upp[5];
730     D(bug(" Get1Resource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
731     *(uint32 *)XLM_GET_1_RESOURCE = tvec[0];
732 gbeauche 1.3 #if EMULATED_PPC
733     tvec[0] = POWERPC_NATIVE_OP_FUNC(NATIVE_GET_1_RESOURCE);
734     #else
735 cebix 1.1 #ifdef __BEOS__
736     tvec2 = (uint32 *)get_1_resource;
737     tvec[0] = tvec2[0];
738     tvec[1] = tvec2[1];
739     #else
740     tvec[0] = (uint32)get_1_resource;
741     #endif
742 gbeauche 1.3 #endif
743 cebix 1.1
744     // Patch native GetIndResource()
745     upp = *(uint32 ***)0x1474;
746     tvec = upp[5];
747     D(bug(" GetIndResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
748     *(uint32 *)XLM_GET_IND_RESOURCE = tvec[0];
749 gbeauche 1.3 #if EMULATED_PPC
750     tvec[0] = POWERPC_NATIVE_OP_FUNC(NATIVE_GET_IND_RESOURCE);
751     #else
752 cebix 1.1 #ifdef __BEOS__
753     tvec2 = (uint32 *)get_ind_resource;
754     tvec[0] = tvec2[0];
755     tvec[1] = tvec2[1];
756     #else
757     tvec[0] = (uint32)get_ind_resource;
758     #endif
759 gbeauche 1.3 #endif
760 cebix 1.1
761     // Patch native Get1IndResource()
762     upp = *(uint32 ***)0xe38;
763     tvec = upp[5];
764     D(bug(" Get1IndResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
765     *(uint32 *)XLM_GET_1_IND_RESOURCE = tvec[0];
766 gbeauche 1.3 #if EMULATED_PPC
767     tvec[0] = POWERPC_NATIVE_OP_FUNC(NATIVE_GET_1_IND_RESOURCE);
768     #else
769 cebix 1.1 #ifdef __BEOS__
770     tvec2 = (uint32 *)get_1_ind_resource;
771     tvec[0] = tvec2[0];
772     tvec[1] = tvec2[1];
773     #else
774     tvec[0] = (uint32)get_1_ind_resource;
775     #endif
776 gbeauche 1.3 #endif
777 cebix 1.1
778     // Patch native RGetResource()
779     upp = *(uint32 ***)0xe30;
780     tvec = upp[5];
781     D(bug(" RGetResource() entry %08x, TOC %08x\n", tvec[0], tvec[1]));
782     *(uint32 *)XLM_R_GET_RESOURCE = tvec[0];
783 gbeauche 1.3 #if EMULATED_PPC
784     tvec[0] = POWERPC_NATIVE_OP_FUNC(NATIVE_R_GET_RESOURCE);
785     #else
786 cebix 1.1 #ifdef __BEOS__
787     tvec2 = (uint32 *)r_get_resource;
788     tvec[0] = tvec2[0];
789     tvec[1] = tvec2[1];
790     #else
791     tvec[0] = (uint32)r_get_resource;
792 gbeauche 1.3 #endif
793 cebix 1.1 #endif
794     }