4 * header for Open Hack'Ware
6 * Copyright (c) 2004-2005 Jocelyn Mayer
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License V2
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #if !defined (__BIOS_H__)
24 #define USE_OPENFIRMWARE
25 //#define DEBUG_BIOS 1
27 #define BIOS_VERSION "0.4.1"
34 #define _tostring(s) #s
35 #define stringify(s) _tostring(s)
37 #if !defined (ASSEMBLY_CODE)
40 #define DPRINTF(fmt, args...) do { dprintf(fmt , ##args); } while (0)
42 #define DPRINTF(fmt, args...) do { } while (0)
44 #define ERROR(fmt, args...) do { printf("ERROR: " fmt , ##args); } while (0)
45 #define MSG(fmt, args...) do { printf(fmt , ##args); } while (0)
47 #define offsetof(_struct, field) \
49 typeof(_struct) __tmp_struct; \
51 __off = (char *)(&__tmp_struct.field) - (char *)(&__tmp_struct); \
55 #define unused __attribute__ (( unused))
57 /* Useful macro in C code */
58 #define MTSPR(num, value) \
59 __asm__ __volatile__ ("mtspr " stringify(num) ", %0" :: "r"(value));
69 /* Hardware definition(s) */
70 extern uint32_t isa_io_base
;
71 #define ISA_IO_BASE 0x80000000
74 /*****************************************************************************/
75 /* From start.S : BIOS start code and asm helpers */
76 void transfer_handler (void *residual
, void *load_addr
,
77 void *OF_entry
, void *bootinfos
,
78 void *cmdline
, void *not_used
,
79 void *nip
, void *stack_base
);
83 uint32_t mfmsr (void);
84 void mtmsr (uint32_t msr
);
85 uint32_t mfpvr (void);
86 void mftb (uint32_t *tb
);
90 uint32_t inb (uint16_t port
);
91 void outb (uint16_t port
, uint32_t val
);
92 uint32_t inw (uint16_t port
);
93 void outw (uint16_t port
, uint32_t val
);
94 uint32_t inl (uint16_t port
);
95 void outl (uint16_t port
, uint32_t val
);
98 uint16_t ldswap16 (uint16_t *addr
);
99 void stswap16 (void *addr
, uint16_t val
);
100 uint32_t ldswap32 (uint32_t *addr
);
101 void stswap32 (void *addr
, uint32_t val
);
102 void mul64 (uint32_t *ret
, uint32_t a
, uint32_t b
);
103 void add64 (uint32_t *ret
, uint32_t *a
, uint32_t *b
);
105 typedef struct jmp_buf {
112 int setjmp (jmp_buf env
);
113 void longjmp (jmp_buf env
);
115 /*****************************************************************************/
117 typedef struct pci_common_t pci_common_t
;
118 typedef struct pci_host_t pci_host_t
;
119 typedef struct pci_device_t pci_device_t
;
120 typedef struct pci_bridge_t pci_bridge_t
;
121 typedef struct pci_ops_t pci_ops_t
;
122 typedef union pci_u_t pci_u_t
;
124 typedef struct pci_dev_t pci_dev_t
;
128 const unsigned char *type
;
129 const unsigned char *name
;
130 const unsigned char *model
;
131 const unsigned char *compat
;
135 int (*config_cb
)(pci_device_t
*device
);
139 pci_host_t
*pci_init (void);
140 void pci_get_mem_range (pci_host_t
*host
, uint32_t *start
, uint32_t *len
);
142 /*****************************************************************************/
143 /* nvram.c : NVRAM management routines */
144 typedef struct nvram_t nvram_t
;
145 extern nvram_t
*nvram
;
147 uint8_t NVRAM_read (nvram_t
*nvram
, uint32_t addr
);
148 void NVRAM_write (nvram_t
*nvram
, uint32_t addr
, uint8_t value
);
149 uint16_t NVRAM_get_size (nvram_t
*nvram
);
150 int NVRAM_format (nvram_t
*nvram
);
151 nvram_t
*NVRAM_get_config (uint32_t *RAM_size
, int *boot_device
,
152 void **boot_image
, uint32_t *boot_size
,
153 void **cmdline
, uint32_t *cmdline_size
,
154 void **ramdisk
, uint32_t *ramdisk_size
);
156 /*****************************************************************************/
157 /* bloc.c : bloc devices management */
158 typedef struct pos_t
{
163 typedef struct bloc_device_t bloc_device_t
;
164 typedef struct part_t part_t
;
165 typedef struct fs_t fs_t
;
167 bloc_device_t
*bd_open (int device
);
168 int bd_seek (bloc_device_t
*bd
, uint32_t bloc
, uint32_t pos
);
169 int bd_read (bloc_device_t
*bd
, void *buffer
, int len
);
170 int bd_write (bloc_device_t
*bd
, const void *buffer
, int len
);
171 #define _IOCTL(a, b) (((a) << 16) | (b))
172 #define MEM_SET_ADDR _IOCTL('M', 0x00)
173 #define MEM_SET_SIZE _IOCTL('M', 0x01)
174 int bd_ioctl (bloc_device_t
*bd
, int func
, void *args
);
175 uint32_t bd_seclen (bloc_device_t
*bd
);
176 void bd_close (bloc_device_t
*bd
);
177 uint32_t bd_seclen (bloc_device_t
*bd
);
178 uint32_t bd_maxbloc (bloc_device_t
*bd
);
179 void bd_sect2CHS (bloc_device_t
*bd
, uint32_t secnum
,
180 int *cyl
, int *head
, int *sect
);
181 uint32_t bd_CHS2sect (bloc_device_t
*bd
,
182 int cyl
, int head
, int sect
);
183 part_t
*bd_probe (int boot_device
);
184 bloc_device_t
*bd_get (int device
);
185 void bd_put (bloc_device_t
*bd
);
186 void bd_set_boot_part (bloc_device_t
*bd
, part_t
*partition
);
187 part_t
**_bd_parts (bloc_device_t
*bd
);
189 void ide_pci_pc_register (uint32_t io_base0
, uint32_t io_base1
,
190 uint32_t io_base2
, uint32_t io_base3
,
192 void ide_pci_pmac_register (uint32_t io_base0
, uint32_t io_base1
,
195 /*****************************************************************************/
196 /* part.c : partitions management */
198 PART_TYPE_RAW
= 0x0000,
199 PART_TYPE_PREP
= 0x0001,
200 PART_TYPE_APPLE
= 0x0002,
201 PART_TYPE_ISO9660
= 0x0004,
202 PART_FLAG_DUMMY
= 0x0010,
203 PART_FLAG_DRIVER
= 0x0020,
204 PART_FLAG_PATCH
= 0x0040,
205 PART_FLAG_FS
= 0x0080,
206 PART_FLAG_BOOT
= 0x0100,
214 part_t
*part_open (bloc_device_t
*bd
,
215 uint32_t start
, uint32_t size
, uint32_t spb
);
216 int part_seek (part_t
*part
, uint32_t bloc
, uint32_t pos
);
217 int part_read (part_t
*part
, void *buffer
, int len
);
218 int part_write (part_t
*part
, const void *buffer
, int len
);
219 void part_close (part_t
*part
);
220 uint32_t part_blocsize (part_t
*part
);
221 uint32_t part_flags (part_t
*part
);
222 uint32_t part_size (part_t
*part
);
223 fs_t
*part_fs (part_t
*part
);
225 part_t
*part_get (bloc_device_t
*bd
, int partnum
);
226 part_t
*part_probe (bloc_device_t
*bd
, int set_raw
);
227 int part_set_boot_file (part_t
*part
, uint32_t start
, uint32_t offset
,
230 /*****************************************************************************/
231 /* fs.c : file system management */
232 typedef struct dir_t dir_t
;
233 typedef struct dirent_t dirent_t
;
234 typedef struct inode_t inode_t
;
239 const unsigned char *dname
;
243 INODE_TYPE_UNKNOWN
= 0x00FF,
244 INODE_TYPE_DIR
= 0x0000,
245 INODE_TYPE_FILE
= 0x0001,
246 INODE_TYPE_OTHER
= 0x0002,
247 INODE_TYPE_MASK
= 0x00FF,
248 INODE_FLAG_EXEC
= 0x0100,
249 INODE_FLAG_BOOT
= 0x0200,
250 INODE_FLAG_MASK
= 0xFF00,
253 /* probe a filesystem from a partition */
254 fs_t
*fs_probe (part_t
*part
, int set_raw
);
255 part_t
*fs_part (fs_t
*fs
);
256 /* Recurse thru directories */
257 dir_t
*fs_opendir (fs_t
*fs
, const unsigned char *name
);
258 dirent_t
*fs_readdir (dir_t
*dir
);
259 unsigned char *fs_get_path (dirent_t
*dirent
);
260 void fs_closedir (dir_t
*dir
);
261 /* Play with files */
262 inode_t
*fs_open (fs_t
*fs
, const unsigned char *name
);
263 int fs_seek (inode_t
*inode
, uint32_t bloc
, uint32_t pos
);
264 int fs_read (inode_t
*inode
, void *buffer
, int len
);
265 int fs_write (inode_t
*inode
, const void *buffer
, unused
int len
);
266 void fs_close (inode_t
*inode
);
267 uint32_t fs_get_type (fs_t
*fs
);
268 uint32_t fs_inode_get_type (inode_t
*inode
);
269 uint32_t fs_inode_get_flags (inode_t
*inode
);
270 part_t
*fs_inode_get_part (inode_t
*inode
);
273 unsigned char *fs_get_boot_dirname (fs_t
*fs
);
274 inode_t
*fs_get_bootfile (fs_t
*fs
);
275 int fs_raw_set_bootfile (part_t
*part
,
276 uint32_t start_bloc
, uint32_t start_offset
,
277 uint32_t size_bloc
, uint32_t size_offset
);
279 /*****************************************************************************/
280 /* file.c : file management */
281 #define DEFAULT_LOAD_DEST 0x00100000
283 uint32_t file_seek (inode_t
*file
, uint32_t pos
);
285 /* Executable files loader */
286 int bootfile_load (void **dest
, void **entry
, void **end
,
287 part_t
*part
, int type
, const unsigned char *fname
,
290 /*****************************************************************************/
291 /* char.c : char devices */
292 typedef struct chardev_t chardev_t
;
293 typedef struct cops_t cops_t
;
296 int (*open
)(void *private);
297 int (*close
)(void *private);
298 int (*read
)(void *private);
299 int (*write
)(void *private, int c
);
300 /* Won't implement seek for now */
311 int chardev_register (int type
, cops_t
*ops
, void *private);
312 int chardev_open (chardev_t
*dev
);
313 int chardev_close (chardev_t
*dev
);
314 int chardev_read (chardev_t
*dev
, void *buffer
, int maxlen
);
315 int chardev_write (chardev_t
*dev
, const void *buffer
, int maxlen
);
316 int chardev_type (chardev_t
*dev
);
319 int console_open (void);
320 int console_read (void *buffer
, int maxlen
);
321 int console_write (const void *buffer
, int len
);
322 void console_close (void);
325 #define SERIAL_OUT_PORT (0x03F8)
326 int pc_serial_register (uint16_t base
);
329 typedef struct cuda_t cuda_t
;
330 cuda_t
*cuda_init (uint32_t base
);
331 void cuda_reset (cuda_t
*cuda
);
333 /*****************************************************************************/
334 /* vga.c : VGA console */
335 extern unsigned long vga_fb_phys_addr
;
336 extern int vga_fb_width
;
337 extern int vga_fb_height
;
338 extern int vga_fb_linesize
;
339 extern int vga_fb_bpp
;
340 extern int vga_fb_depth
;
341 void vga_prep_init(void);
342 void vga_set_address (uint32_t address
);
343 void vga_set_mode(int width
, int height
, int depth
);
344 void vga_set_palette(int i
, unsigned int rgba
);
345 #define RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
346 #define RGB(r, g, b) RGBA(r, g, b, 0xff)
347 unsigned int vga_get_color(unsigned int rgba
);
349 void vga_draw_buf (const void *buf
, int buf_linesize
,
350 int posx
, int posy
, int width
, int height
);
351 void vga_fill_rect (int posx
, int posy
, int width
, int height
, uint32_t color
);
352 void vga_bitblt(int xs
, int ys
, int xd
, int yd
, int w
, int h
);
353 void vga_check_mode(int width
, int height
, int depth
);
355 /* text primitives */
356 void vga_text_set_fgcol(unsigned int rgba
);
357 void vga_text_set_bgcol(unsigned int rgba
);
358 void vga_putcharxy(int x
, int y
, int ch
,
359 unsigned int fgcol
, unsigned int bgcol
);
360 void vga_putchar(int ch
);
361 void vga_puts(const char *s
);
363 /*****************************************************************************/
364 /* bootinfos.c : build structures needed by kernels to boot */
365 void prepare_bootinfos (void *p
, uint32_t memsize
,
366 void *cmdline
, void *initrd
, uint32_t initrd_size
);
367 void residual_build (void *p
, uint32_t memsize
,
368 uint32_t load_base
, uint32_t load_size
,
369 uint32_t last_alloc
);
371 /*****************************************************************************/
372 /* of.c : Open-firmware emulation */
373 #define OF_NAMELEN_MAX 1024
374 #define OF_PROPLEN_MAX 256
377 int OF_register_mb (const unsigned char *model
, const unsigned char **compats
);
378 int OF_register_cpu (const unsigned char *name
, int num
, uint32_t pvr
,
379 uint32_t min_freq
, uint32_t max_freq
, uint32_t bus_freq
,
380 uint32_t tb_freq
, uint32_t reset_io
);
382 int OF_register_translations (int nb
, OF_transl_t
*translations
);
384 uint32_t OF_claim_virt (uint32_t virt
, uint32_t size
, int *range
);
385 int OF_register_memory (uint32_t memsize
, uint32_t bios_size
);
386 int OF_register_bootargs (const unsigned char *bootargs
);
387 void *OF_register_pci_host (pci_dev_t
*dev
, uint16_t rev
, uint32_t ccode
,
388 uint32_t cfg_base
, uint32_t cfg_len
,
389 uint32_t mem_base
, uint32_t mem_len
,
390 uint32_t io_base
, uint32_t io_len
,
391 uint32_t rbase
, uint32_t rlen
,
392 uint16_t min_grant
, uint16_t max_latency
);
393 void *OF_register_pci_bridge (void *parent
, pci_dev_t
*dev
,
394 uint32_t cfg_base
, uint32_t cfg_len
,
395 uint8_t devfn
, uint8_t rev
, uint32_t ccode
,
396 uint16_t min_grant
, uint16_t max_latency
);
397 void *OF_register_pci_device (void *parent
, pci_dev_t
*dev
,
398 uint8_t devfn
, uint8_t rev
, uint32_t ccode
,
399 uint16_t min_grant
, uint16_t max_latency
);
400 void OF_finalize_pci_host (void *dev
, int first_bus
, int nb_busses
);
401 void OF_finalize_pci_device (void *dev
, uint8_t bus
, uint8_t devfn
,
402 uint32_t *regions
, uint32_t *sizes
);
403 void OF_finalize_pci_macio (void *dev
, uint32_t base_address
, uint32_t size
,
405 int OF_register_bus (const unsigned char *name
, uint32_t address
,
406 const unsigned char *type
);
407 int OF_register_serial (const unsigned char *bus
, const unsigned char *name
,
408 uint32_t io_base
, int irq
);
409 int OF_register_stdio (const unsigned char *dev_in
,
410 const unsigned char *dev_out
);
411 void OF_vga_register (const unsigned char *name
, uint32_t address
,
412 int width
, int height
, int depth
);
413 void *OF_blockdev_register (void *parent
, void *private,
414 const unsigned char *type
,
415 const unsigned char *name
, int devnum
,
417 void OF_blockdev_set_boot_device (void *disk
, int partnum
,
418 const unsigned char *file
);
420 int OF_entry (void *p
);
421 int OF_client_entry (void *p
);
422 void RTAS_init (void);
424 /*****************************************************************************/
425 /* main.c : main BIOS code */
426 /* Memory management */
428 extern uint32_t _data_start
, _data_end
;
429 extern uint32_t _OF_vars_start
, _OF_vars_end
;
430 extern uint32_t _sdata_start
, _sdata_end
;
431 extern uint32_t _ro_start
, _ro_end
;
432 extern uint32_t _RTAS_start
, _RTAS_end
;
433 extern uint32_t _RTAS_data_start
, _RTAS_data_end
;
434 extern uint32_t _bss_start
, _bss_end
;
435 extern uint32_t _ram_start
;
436 extern const unsigned char *BIOS_str
;
437 extern const unsigned char *copyright
;
438 void *mem_align (int align
);
439 void freep (void *p
);
441 /* Endian-safe memory read/write */
442 static inline void put_be64 (void *addr
, uint64_t l
)
444 *(uint64_t *)addr
= l
;
447 static inline uint64_t get_be64 (void *addr
)
449 return *(uint64_t *)addr
;
452 static inline void put_le64 (void *addr
, uint64_t l
)
458 stswap32(p
+ 1, l
>> 32);
461 static inline uint64_t get_le64 (void *addr
)
468 val
|= (uint64_t)ldswap32(p
+ 1) << 32;
473 static inline void put_be32 (void *addr
, uint32_t l
)
475 *(uint32_t *)addr
= l
;
478 static inline uint32_t get_be32 (void *addr
)
480 return *(uint32_t *)addr
;
483 static inline void put_le32 (void *addr
, uint32_t l
)
488 static inline uint32_t get_le32 (void *addr
)
490 return ldswap32(addr
);
493 static inline void put_be16 (void *addr
, uint16_t l
)
495 *(uint16_t *)addr
= l
;
498 static inline uint16_t get_be16 (void *addr
)
500 return *(uint16_t *)addr
;
503 static inline void put_le16 (void *addr
, uint16_t l
)
508 static inline uint16_t get_le16 (void *addr
)
510 return ldswap16(addr
);
513 /* String functions */
514 long strtol (const unsigned char *str
, unsigned char **end
, int base
);
516 int write_buf (const unsigned char *buf
, int len
);
519 void usleep (uint32_t usec
);
520 void sleep (int sec
);
521 uint32_t crc32 (uint32_t crc
, const uint8_t *p
, int len
);
522 void set_loadinfo (void *load_base
, uint32_t size
);
523 void set_check (int do_it
);
524 void check_location (const void *buf
, const char *func
, const char *name
);
526 static inline void pokeb (void *location
, uint8_t val
)
529 check_location(location
, __func__
, "location");
531 *((uint8_t *)location
) = val
;
534 static inline uint8_t peekb (void *location
)
537 check_location(location
, __func__
, "location");
539 return *((uint8_t *)location
);
542 static inline void pokew (void *location
, uint16_t val
)
545 check_location(location
, __func__
, "location");
547 *((uint8_t *)location
) = val
;
550 static inline uint16_t peekw (void *location
)
553 check_location(location
, __func__
, "location");
555 return *((uint16_t *)location
);
558 static inline void pokel (void *location
, uint32_t val
)
561 check_location(location
, __func__
, "location");
563 *((uint32_t *)location
) = val
;
566 static inline uint32_t peekl (void *location
)
569 check_location(location
, __func__
, "location");
571 return *((uint32_t *)location
);
575 int cs_write (const unsigned char *buf
, int len
);
577 #endif /* !defined (ASSEMBLY_CODE) */
580 #endif /* !defined (__BIOS_H__) */