1 /*---------------------------------------------------------------------------*
2   Project:      Utilities for sample programs of NAND library
3   File:         util.c
4   Programmer:   HIRATSU Daisuke
5 
6   Copyright (C) 2006 Nintendo.  All rights reserved.
7 
8   These coded instructions, statements, and computer programs contain
9   proprietary information of Nintendo of America Inc. and/or Nintendo
10   Company Ltd., and are protected by Federal copyright law.  They may
11   not be disclosed to third parties or copied or duplicated in any form,
12   in whole or in part, without the prior written consent of Nintendo.
13 
14   $Log: util.c,v $
15   Revision 1.8  07/27/2006 05:55:57  hiratsu
16   Removed outdated error code: NAND_RESULT_INIT_FAILED.
17 
18   Revision 1.7  07/26/2006 08:39:24  hiratsu
19   Renamed error code.
20 
21   Revision 1.6  07/24/2006 07:22:21  hiratsu
22   Added new error code.
23 
24   Revision 1.5  05/09/2006 09:09:14  hiratsu
25   Modified error code handling.
26 
27   Revision 1.4  03/17/2006 02:39:36  hiratsu
28   Implemented MEM library wrapper in order to allocate buffer from MEM2 with 32byte alignment.
29 
30   Revision 1.3  03/16/2006 11:52:30  hiratsu
31   New error code is arrived.  Fixed.
32 
33   Revision 1.2  01/30/2006 10:33:15  hiratsu
34   Now this file includes "nand.h", not "nandEmu.h".
35 
36   Revision 1.1  01/24/2006 12:35:17  hiratsu
37   Utility functions for NAND library demo program.
38 
39  *---------------------------------------------------------------------------*/
40 
41 
42 #include "util.h"
43 
44 #include <revolution.h>
45 #include <revolution/nand.h>
46 #include <revolution/mem.h>
47 
48 static MEMAllocator	s_mem2Allocator;
49 static MEMHeapHandle s_handle;
50 
51 
initializeHeap(void)52 void initializeHeap(void)
53 {
54     void *lo = OSGetMEM2ArenaLo();
55     void *hi = OSGetMEM2ArenaHi();
56     s_handle = MEMCreateFrmHeap(lo, (u32)hi - (u32)lo);
57     if ( s_handle == MEM_HEAP_INVALID_HANDLE )
58     {
59         OSHalt("MEM2 heap allocation error.\n");
60     }
61     else
62     {
63         OSSetMEM2ArenaLo(hi);
64         MEMInitAllocatorForFrmHeap(&s_mem2Allocator, s_handle, 32);   // buffer requires 32byte alignment.
65     }
66 }
67 
68 
finalizeHeap(void)69 void finalizeHeap(void)
70 {
71     MEMDestroyFrmHeap(s_handle);
72 }
73 
74 
alloc32(u32 size)75 void* alloc32(u32 size)
76 {
77     return MEMAllocFromAllocator(&s_mem2Allocator, size);
78 
79 }
80 
81 
free32(void * addr)82 void free32(void *addr)
83 {
84     MEMFreeToAllocator(&s_mem2Allocator, addr);
85 }
86 
87 
printErrMsg(s32 err)88 void printErrMsg(s32 err)
89 {
90     switch(err)
91     {
92     case NAND_RESULT_ACCESS:
93         OSReport("NAND_RESULT_ACCESS\n");
94         break;
95     case NAND_RESULT_ALLOC_FAILED:
96         OSReport("NAND_RESULT_ALLOC_FAILED\n");
97         break;
98     case NAND_RESULT_BUSY:
99         OSReport("NAND_RESULT_BUSY\n");
100         break;
101     case NAND_RESULT_CORRUPT:
102         OSReport("NAND_RESULT_CORRUPT\n");
103         break;
104     case NAND_RESULT_ECC_CRIT:
105         OSReport("NAND_RESULT_ECC_CRIT\n");
106         break;
107     case NAND_RESULT_EXISTS:
108         OSReport("NAND_RESULT_EXISTS\n");
109         break;
110     case NAND_RESULT_INVALID:
111         OSReport("NAND_RESULT_INVALID\n");
112         break;
113     case NAND_RESULT_MAXBLOCKS:
114         OSReport("NAND_RESULT_MAXBLOCKS\n");
115         break;
116     case NAND_RESULT_MAXFD:
117         OSReport("NAND_RESULT_MAXFD\n");
118         break;
119     case NAND_RESULT_MAXFILES:
120         OSReport("NAND_RESULT_MAXFILES\n");
121         break;
122     case NAND_RESULT_NOEXISTS:
123         OSReport("NAND_RESULT_NOEXISTS\n");
124         break;
125     case NAND_RESULT_NOTEMPTY:
126         OSReport("NAND_RESULT_NOTEMPTY\n");
127         break;
128     case NAND_RESULT_OPENFD:
129         OSReport("NAND_RESULT_OPENFD\n");
130         break;
131     case NAND_RESULT_AUTHENTICATION:
132         OSReport("NAND_RESULT_AUTHENTICATION\n");
133         break;
134     case NAND_RESULT_UNKNOWN:
135         OSReport("NAND_RESULT_UNKNOWN\n");
136         break;
137     case NAND_RESULT_FATAL_ERROR:
138         OSReport("NAND_RESULT_FATAL_ERROR\n");
139         break;
140     default:
141         OSReport("Result: %d\n", err);
142         break;
143     }
144 }
145