blob: 21668332761b16c15bcc03f3bfb76c14a3b522c3 [file] [log] [blame]
Amaury Pouly9d7df9a2011-09-15 16:10:31 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2011 Amaury Pouly
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21#ifndef __ELF_H__
22#define __ELF_H__
23
Amaury Pouly1583b1c2010-12-01 16:17:11 +000024#include <stdio.h>
25#include <stdint.h>
26#include <string.h>
27#include <stdbool.h>
28#include <stdlib.h>
29#include <unistd.h>
30
31/**
Amaury Pouly1583b1c2010-12-01 16:17:11 +000032 * API
33 */
34enum elf_section_type_t
35{
36 EST_LOAD,
37 EST_FILL
38};
39
40struct elf_section_t
41{
Amaury Pouly64b46722011-09-15 14:36:58 +000042 uint32_t addr; /* virtual address */
43 uint32_t size; /* virtual size */
Amaury Pouly1583b1c2010-12-01 16:17:11 +000044 enum elf_section_type_t type;
45 /* <union> */
Amaury Pouly64b46722011-09-15 14:36:58 +000046 void *section; /* data */
47 uint32_t pattern; /* fill pattern */
Amaury Pouly1583b1c2010-12-01 16:17:11 +000048 /* </union> */
49 struct elf_section_t *next;
Amaury Poulyc4cb4cc2011-04-17 15:49:58 +000050 /* Internal to elf_write_file */
Amaury Pouly1583b1c2010-12-01 16:17:11 +000051 uint32_t offset;
52};
53
Amaury Pouly64b46722011-09-15 14:36:58 +000054struct elf_segment_t
55{
56 uint32_t vaddr; /* virtual address */
57 uint32_t paddr; /* physical address */
58 uint32_t vsize; /* virtual size */
59 uint32_t psize; /* physical size */
60 struct elf_segment_t *next;
61};
62
Amaury Pouly1583b1c2010-12-01 16:17:11 +000063struct elf_params_t
64{
65 bool has_start_addr;
66 uint32_t start_addr;
67 struct elf_section_t *first_section;
68 struct elf_section_t *last_section;
Amaury Pouly64b46722011-09-15 14:36:58 +000069 struct elf_segment_t *first_segment;
70 struct elf_segment_t *last_segment;
Amaury Pouly1583b1c2010-12-01 16:17:11 +000071};
72
Amaury Poulyc4cb4cc2011-04-17 15:49:58 +000073typedef bool (*elf_read_fn_t)(void *user, uint32_t addr, void *buf, size_t count);
74/* write function manages it's own error state */
Amaury Pouly1583b1c2010-12-01 16:17:11 +000075typedef void (*elf_write_fn_t)(void *user, uint32_t addr, const void *buf, size_t count);
Amaury Poulyc4cb4cc2011-04-17 15:49:58 +000076typedef void (*elf_printf_fn_t)(void *user, bool error, const char *fmt, ...);
Amaury Pouly1583b1c2010-12-01 16:17:11 +000077
78void elf_init(struct elf_params_t *params);
79void elf_add_load_section(struct elf_params_t *params,
80 uint32_t load_addr, uint32_t size, const void *section);
81void elf_add_fill_section(struct elf_params_t *params,
82 uint32_t fill_addr, uint32_t size, uint32_t pattern);
Amaury Pouly64b46722011-09-15 14:36:58 +000083uint32_t elf_translate_virtual_address(struct elf_params_t *params, uint32_t addr);
84void elf_translate_addresses(struct elf_params_t *params);
Amaury Pouly4cab9f22011-10-29 22:27:53 +000085void elf_write_file(struct elf_params_t *params, elf_write_fn_t write, elf_printf_fn_t printf, void *user);
Amaury Poulyc4cb4cc2011-04-17 15:49:58 +000086bool elf_read_file(struct elf_params_t *params, elf_read_fn_t read, elf_printf_fn_t printf,
87 void *user);
Amaury Pouly1583b1c2010-12-01 16:17:11 +000088bool elf_is_empty(struct elf_params_t *params);
89void elf_set_start_addr(struct elf_params_t *params, uint32_t addr);
Amaury Poulyc4cb4cc2011-04-17 15:49:58 +000090bool elf_get_start_addr(struct elf_params_t *params, uint32_t *addr);
91int elf_get_nr_sections(struct elf_params_t *params);
Amaury Pouly1583b1c2010-12-01 16:17:11 +000092void elf_release(struct elf_params_t *params);
Amaury Pouly9d7df9a2011-09-15 16:10:31 +000093
94#endif /* __ELF_H__ */