1 /** @file
2 
3   Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
4   Copyright (c) 2013 - 2014, ARM Ltd. All rights reserved.<BR>
5 
6   This program and the accompanying materials
7   are licensed and made available under the terms and conditions of the BSD License
8   which accompanies this distribution.  The full text of the license may be found at
9   http://opensource.org/licenses/bsd-license.php
10 
11   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 
14 **/
15 
16 #ifndef __SEMIHOST_PRIVATE_H__
17 #define __SEMIHOST_PRIVATE_H__
18 
19 typedef struct {
20   CHAR8   *FileName;
21   UINTN    Mode;
22   UINTN    NameLength;
23 } SEMIHOST_FILE_OPEN_BLOCK;
24 
25 typedef struct {
26   UINTN    Handle;
27   VOID    *Buffer;
28   UINTN    Length;
29 } SEMIHOST_FILE_READ_WRITE_BLOCK;
30 
31 typedef struct {
32   UINTN    Handle;
33   UINTN    Location;
34 } SEMIHOST_FILE_SEEK_BLOCK;
35 
36 typedef struct {
37   VOID    *Buffer;
38   UINTN    Identifier;
39   UINTN    Length;
40 } SEMIHOST_FILE_TMPNAME_BLOCK;
41 
42 typedef struct {
43   CHAR8   *FileName;
44   UINTN    NameLength;
45 } SEMIHOST_FILE_REMOVE_BLOCK;
46 
47 typedef struct {
48   CHAR8   *FileName;
49   UINTN    FileNameLength;
50   CHAR8   *NewFileName;
51   UINTN    NewFileNameLength;
52 } SEMIHOST_FILE_RENAME_BLOCK;
53 
54 typedef struct {
55   CHAR8   *CommandLine;
56   UINTN    CommandLength;
57 } SEMIHOST_SYSTEM_BLOCK;
58 
59 #if defined(__CC_ARM)
60 
61 #if defined(__thumb__)
62 #define SWI 0xAB
63 #else
64 #define SWI 0x123456
65 #endif
66 
67 #define SEMIHOST_SUPPORTED  TRUE
68 
69 __swi(SWI)
70 INT32
71 _Semihost_SYS_OPEN(
72   IN UINTN                    SWI_0x01,
73   IN SEMIHOST_FILE_OPEN_BLOCK *OpenBlock
74   );
75 
76 __swi(SWI)
77 INT32
78 _Semihost_SYS_CLOSE(
79   IN UINTN  SWI_0x02,
80   IN UINT32 *Handle
81   );
82 
83 __swi(SWI)
84 VOID
85 _Semihost_SYS_WRITEC(
86   IN UINTN    SWI_0x03,
87   IN CHAR8    *Character
88   );
89 
90 __swi(SWI)
91 VOID
92 _Semihost_SYS_WRITE0(
93   IN UINTN SWI_0x04,
94   IN CHAR8 *String
95   );
96 
97 __swi(SWI)
98 UINT32
99 _Semihost_SYS_WRITE(
100   IN     UINTN                          SWI_0x05,
101   IN OUT SEMIHOST_FILE_READ_WRITE_BLOCK *WriteBlock
102   );
103 
104 __swi(SWI)
105 UINT32
106 _Semihost_SYS_READ(
107   IN     UINTN                          SWI_0x06,
108   IN OUT SEMIHOST_FILE_READ_WRITE_BLOCK *ReadBlock
109   );
110 
111 __swi(SWI)
112 CHAR8
113 _Semihost_SYS_READC(
114   IN     UINTN SWI_0x07,
115   IN     UINTN Zero
116   );
117 
118 __swi(SWI)
119 INT32
120 _Semihost_SYS_SEEK(
121   IN UINTN                    SWI_0x0A,
122   IN SEMIHOST_FILE_SEEK_BLOCK *SeekBlock
123   );
124 
125 __swi(SWI)
126 INT32
127 _Semihost_SYS_FLEN(
128   IN UINTN  SWI_0x0C,
129   IN UINT32 *Handle
130   );
131 
132 __swi(SWI)
133 UINT32
134 _Semihost_SYS_TMPNAME(
135   IN UINTN                       SWI_0x0D,
136   IN SEMIHOST_FILE_TMPNAME_BLOCK *TmpNameBlock
137   );
138 
139 __swi(SWI)
140 UINT32
141 _Semihost_SYS_REMOVE(
142   IN UINTN                      SWI_0x0E,
143   IN SEMIHOST_FILE_REMOVE_BLOCK *RemoveBlock
144   );
145 
146 __swi(SWI)
147 UINT32
148 _Semihost_SYS_RENAME(
149   IN UINTN                      SWI_0x0F,
150   IN SEMIHOST_FILE_RENAME_BLOCK *RenameBlock
151   );
152 
153 __swi(SWI)
154 UINT32
155 _Semihost_SYS_SYSTEM(
156   IN UINTN                 SWI_0x12,
157   IN SEMIHOST_SYSTEM_BLOCK *SystemBlock
158   );
159 
160 #define Semihost_SYS_OPEN(OpenBlock)        _Semihost_SYS_OPEN(0x01, OpenBlock)
161 #define Semihost_SYS_CLOSE(Handle)          _Semihost_SYS_CLOSE(0x02, Handle)
162 #define Semihost_SYS_WRITE0(String)         _Semihost_SYS_WRITE0(0x04, String)
163 #define Semihost_SYS_WRITEC(Character)      _Semihost_SYS_WRITEC(0x03, Character)
164 #define Semihost_SYS_WRITE(WriteBlock)      _Semihost_SYS_WRITE(0x05, WriteBlock)
165 #define Semihost_SYS_READ(ReadBlock)        _Semihost_SYS_READ(0x06, ReadBlock)
166 #define Semihost_SYS_READC()                _Semihost_SYS_READC(0x07, 0)
167 #define Semihost_SYS_SEEK(SeekBlock)        _Semihost_SYS_SEEK(0x0A, SeekBlock)
168 #define Semihost_SYS_FLEN(Handle)           _Semihost_SYS_FLEN(0x0C, Handle)
169 #define Semihost_SYS_TMPNAME(TmpNameBlock)  _Semihost_SYS_TMPNAME(0x0D, TmpNameBlock)
170 #define Semihost_SYS_REMOVE(RemoveBlock)    _Semihost_SYS_REMOVE(0x0E, RemoveBlock)
171 #define Semihost_SYS_RENAME(RenameBlock)    _Semihost_SYS_RENAME(0x0F, RenameBlock)
172 #define Semihost_SYS_SYSTEM(SystemBlock)    _Semihost_SYS_SYSTEM(0x12, SystemBlock)
173 
174 #elif defined(__GNUC__) // __CC_ARM
175 
176 #define SEMIHOST_SUPPORTED  TRUE
177 
178 UINT32
179 GccSemihostCall (
180   IN UINT32   Operation,
181   IN UINTN    SystemBlockAddress
182   ); // __attribute__ ((interrupt ("SVC")));
183 
184 #define Semihost_SYS_OPEN(OpenBlock)        GccSemihostCall(0x01, (UINTN)(OpenBlock))
185 #define Semihost_SYS_CLOSE(Handle)          GccSemihostCall(0x02, (UINTN)(Handle))
186 #define Semihost_SYS_WRITE0(String)         GccSemihostCall(0x04, (UINTN)(String))
187 #define Semihost_SYS_WRITEC(Character)      GccSemihostCall(0x03, (UINTN)(Character))
188 #define Semihost_SYS_WRITE(WriteBlock)      GccSemihostCall(0x05, (UINTN)(WriteBlock))
189 #define Semihost_SYS_READ(ReadBlock)        GccSemihostCall(0x06, (UINTN)(ReadBlock))
190 #define Semihost_SYS_READC()                GccSemihostCall(0x07, (UINTN)(0))
191 #define Semihost_SYS_SEEK(SeekBlock)        GccSemihostCall(0x0A, (UINTN)(SeekBlock))
192 #define Semihost_SYS_FLEN(Handle)           GccSemihostCall(0x0C, (UINTN)(Handle))
193 #define Semihost_SYS_TMPNAME(TmpNameBlock)  GccSemihostCall(0x0D, (UINTN)(TmpNameBlock))
194 #define Semihost_SYS_REMOVE(RemoveBlock)    GccSemihostCall(0x0E, (UINTN)(RemoveBlock))
195 #define Semihost_SYS_RENAME(RenameBlock)    GccSemihostCall(0x0F, (UINTN)(RenameBlock))
196 #define Semihost_SYS_SYSTEM(SystemBlock)    GccSemihostCall(0x12, (UINTN)(SystemBlock))
197 
198 #else // __CC_ARM
199 
200 #define SEMIHOST_SUPPORTED  FALSE
201 
202 #define Semihost_SYS_OPEN(OpenBlock)        (-1)
203 #define Semihost_SYS_CLOSE(Handle)          (-1)
204 #define Semihost_SYS_WRITE0(String)
205 #define Semihost_SYS_WRITEC(Character)
206 #define Semihost_SYS_WRITE(WriteBlock)      (0)
207 #define Semihost_SYS_READ(ReadBlock)        ((ReadBlock)->Length)
208 #define Semihost_SYS_READC()                ('x')
209 #define Semihost_SYS_SEEK(SeekBlock)        (-1)
210 #define Semihost_SYS_FLEN(Handle)           (-1)
211 #define Semihost_SYS_TMPNAME(TmpNameBlock)  (-1)
212 #define Semihost_SYS_REMOVE(RemoveBlock)    (-1)
213 #define Semihost_SYS_RENAME(RenameBlock)    (-1)
214 #define Semihost_SYS_SYSTEM(SystemBlock)    (-1)
215 
216 #endif // __CC_ARM
217 
218 #endif //__SEMIHOST_PRIVATE_H__
219