POK(kernelpart)
loader.c
Go to the documentation of this file.
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Thu Jan 15 23:34:13 2009
15  */
16 
29 #ifdef POK_NEEDS_PARTITIONS
30 
31 #include <errno.h>
32 #include <types.h>
33 #include <libc.h>
34 #include <core/cpio.h>
35 #include <core/error.h>
36 #include <core/partition.h>
37 #include <elf.h>
38 
43 extern uint32_t part_sizes[];
44 
49 static pok_ret_t pok_loader_elf_load (char* file,
50  uint32_t offset,
51  void** entry)
52 {
53  Elf32_Ehdr* elf_header;
54  Elf32_Phdr* elf_phdr;
55  unsigned int i;
56  char* dest;
57 
58  elf_header = (Elf32_Ehdr*)file;
59 
60  if (elf_header->e_ident[0] != 0x7f ||
61  elf_header->e_ident[1] != 'E' ||
62  elf_header->e_ident[2] != 'L' ||
63  elf_header->e_ident[3] != 'F')
64  {
65  return POK_ERRNO_NOTFOUND;
66  }
67 
68  *entry = (void*)elf_header->e_entry;
69 
70  elf_phdr = (Elf32_Phdr*)(file + elf_header->e_phoff);
71 
72  for (i = 0; i < elf_header->e_phnum; ++i)
73  {
74  dest = (char *)elf_phdr[i].p_vaddr + offset;
75 
76  memcpy (dest, elf_phdr[i].p_offset + file, elf_phdr[i].p_filesz);
77  memset (dest + elf_phdr[i].p_filesz, 0, elf_phdr[i].p_memsz - elf_phdr[i].p_filesz);
78  }
79 
80  return POK_ERRNO_OK;
81 }
82 
83 
84 
85 void pok_loader_load_partition (const uint8_t part_id,
86  uint32_t offset,
87  uint32_t *entry)
88 {
89  void* elf_entry = NULL;
90  extern char __archive2_begin;
91  uint32_t size;
92  uint8_t t;
93 
94  size = 0;
95  t = 0;
96 
97  while (t < part_id)
98  {
99  size += part_sizes[t];
100  t++;
101  }
102 
103  if (pok_partitions[part_id].size < part_sizes[part_id])
104  {
105 #ifdef POK_NEEDS_ERROR_HANDLING
106  pok_partition_error (part_id, POK_ERROR_KIND_PARTITION_CONFIGURATION);
107 #else
108 #ifdef POK_NEEDS_DEBUG
109  /* We consider that even if errors are not raised, we must print an error
110  * for such error
111  * So, when we are using the debug mode, we print a fatal error.
112  */
113 #include <core/debug.h>
114 #include <libc.h>
115  printf("Declared size for partition %d : %d\n", part_id, pok_partitions[part_id].size);
116  printf("Real size for partition %d : %d\n", part_id, part_sizes[part_id]);
117  pok_fatal ("Partition size is not correct\n");
118 #endif
119 #endif
120  }
121  /*
122  * FIXME : current debug session about exceptions-handled
123  printf ("Will load partition at offset 0x%x\n", offset);
124  */
125  pok_loader_elf_load ((&__archive2_begin) + size , offset, &elf_entry);
126 
127  *entry = (uint32_t)elf_entry;
128 }
129 
130 #endif /* POK_NEEDS_PARTITIONS */