/****************************************************************************** * elf.c * * Generic Elf-loading routines. */ #include #include #include #include #include #include #ifdef CONFIG_X86 #define FORCE_XENELF_IMAGE 1 #elif defined(__ia64__) #define FORCE_XENELF_IMAGE 0 #endif static void loadelfsymtab(struct domain_setup_info *dsi, int doload); static inline int is_loadable_phdr(Elf_Phdr *phdr) { return ((phdr->p_type == PT_LOAD) && ((phdr->p_flags & (PF_W|PF_X)) != 0)); } int parseelfimage(struct domain_setup_info *dsi) { Elf_Ehdr *ehdr = (Elf_Ehdr *)dsi->image_addr; Elf_Phdr *phdr; Elf_Shdr *shdr; unsigned long kernstart = ~0UL, kernend=0UL; char *shstrtab, *guestinfo=NULL, *p; char *elfbase = (char *)dsi->image_addr; int h; if ( !elf_sanity_check(ehdr) ) return -EINVAL; if ( (ehdr->e_phoff + (ehdr->e_phnum*ehdr->e_phentsize)) > dsi->image_len ) { printk("ELF program headers extend beyond end of image.\n"); return -EINVAL; } if ( (ehdr->e_shoff + (ehdr->e_shnum*ehdr->e_shentsize)) > dsi->image_len ) { printk("ELF section headers extend beyond end of image.\n"); return -EINVAL; } /* Find the section-header strings table. */ if ( ehdr->e_shstrndx == SHN_UNDEF ) { printk("ELF image has no section-header strings table (shstrtab).\n"); return -EINVAL; } shdr = (Elf_Shdr *)(elfbase + ehdr->e_shoff + (ehdr->e_shstrndx*ehdr->e_shentsize)); shstrtab = elfbase + shdr->sh_offset; /* Find the special '__xen_guest' section and check its contents. */ for ( h = 0; h < ehdr->e_shnum; h++ ) { shdr = (Elf_Shdr *)(elfbase + ehdr->e_shoff + (h*ehdr->e_shentsize)); if ( strcmp(&shstrtab[shdr->sh_name], "__xen_guest") != 0 ) continue; guestinfo = elfbase + shdr->sh_offset; printk("Xen-ELF header found: '%s'\n", guestinfo); if ( (strstr(guestinfo, "LOADER=generic") == NULL) && (strstr(guestinfo, "GUEST_OS=linux") == NULL) ) { printk("ERROR: Xen will only load images built for the generic " "loader or Linux images\n"); return -EINVAL; } if ( (strstr(guestinfo, "XEN_VER=3.0") == NULL) ) { printk("ERROR: Xen will only load images built for Xen v3.0\n"); return -EINVAL; } break; } if ( guestinfo == NULL ) { printk("Not a Xen-ELF image: '__xen_guest' section not found.\n"); #if FORCE_XENELF_IMAGE return -EINVAL; #endif } for ( h = 0; h < ehdr->e_phnum; h++ ) { phdr = (Elf_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize)); if ( !is_loadable_phdr(phdr) ) continue; if ( phdr->p_paddr < kernstart ) kernstart = phdr->p_paddr; if ( (phdr->p_paddr + phdr->p_memsz) > kernend ) kernend = phdr->p_paddr + phdr->p_memsz; } if ( (kernstart > kernend) || (ehdr->e_entry < kernstart) || (ehdr->e_entry > kernend) ) { printk("Malformed ELF image.\n"); return -EINVAL; } dsi->v_start = kernstart; if ( guestinfo != NULL ) { if ( (p = strstr(guestinfo, "VIRT_BASE=")) != NULL ) dsi->v_start = simple_strtoul(p+10, &p, 0); if ( (p = strstr(guestinfo, "BSD_SYMTAB")) != NULL ) dsi->load_symtab = 1; } dsi->v_kernstart = kernstart; dsi->v_kernend = kernend; dsi->v_kernentry = ehdr->e_entry; dsi->v_end = dsi->v_kernend; loadelfsymtab(dsi, 0); return 0; } int loadelfimage(struct domain_setup_info *dsi) { char *elfbase = (char *)dsi->image_addr; Elf_Ehdr *ehdr = (Elf_Ehdr *)dsi->image_addr; Elf_Phdr *phdr; int h; for ( h = 0; h < ehdr->e_phnum; h++ ) { phdr = (Elf_Phdr *)(elfbase + ehdr->e_phoff + (h*ehdr->e_phentsize)); if ( !is_loadable_phdr(phdr) ) continue; if ( phdr->p_filesz != 0 ) memcpy((char *)phdr->p_paddr, elfbase + phdr->p_offset, phdr->p_filesz); if ( phdr->p_memsz > phdr->p_filesz ) memset((char *)phdr->p_paddr + phdr->p_filesz, 0, phdr->p_memsz - phdr->p_filesz); } loadelfsymtab(dsi, 1); return 0; } #define ELFROUND (ELFSIZE / 8) static void loadelfsymtab(struct domain_setup_info *dsi, int doload) { Elf_Ehdr *ehdr = (Elf_Ehdr *)dsi->image_addr, *sym_ehdr; Elf_Shdr *shdr; unsigned long maxva, symva; char *p, *elfbase = (char *)dsi->image_addr; int h, i; if ( !dsi->load_symtab ) return; maxva = (dsi->v_kernend + ELFROUND - 1) & ~(ELFROUND - 1); symva = maxva; maxva += sizeof(int); dsi->symtab_addr = maxva; dsi->symtab_len = 0; maxva += sizeof(Elf_Ehdr) + ehdr->e_shnum * sizeof(Elf_Shdr); maxva = (maxva + ELFROUND - 1) & ~(ELFROUND - 1); if ( doload ) { p = (void *)symva; shdr = (Elf_Shdr *)(p + sizeof(int) + sizeof(Elf_Ehdr)); memcpy(shdr, elfbase + ehdr->e_shoff, ehdr->e_shnum*sizeof(Elf_Shdr)); } else { p = NULL; shdr = (Elf_Shdr *)(elfbase + ehdr->e_shoff); } for ( h = 0; h < ehdr->e_shnum; h++ ) { if ( shdr[h].sh_type == SHT_STRTAB ) { /* Look for a strtab @i linked to symtab @h. */ for ( i = 0; i < ehdr->e_shnum; i++ ) if ( (shdr[i].sh_type == SHT_SYMTAB) && (shdr[i].sh_link == h) ) break; /* Skip symtab @h if we found no corresponding strtab @i. */ if ( i == ehdr->e_shnum ) { if (doload) { shdr[h].sh_offset = 0; } continue; } } if ( (shdr[h].sh_type == SHT_STRTAB) || (shdr[h].sh_type == SHT_SYMTAB) ) { if (doload) { memcpy((void *)maxva, elfbase + shdr[h].sh_offset, shdr[h].sh_size); /* Mangled to be based on ELF header location. */ shdr[h].sh_offset = maxva - dsi->symtab_addr; } dsi->symtab_len += shdr[h].sh_size; maxva += shdr[h].sh_size; maxva = (maxva + ELFROUND - 1) & ~(ELFROUND - 1); } if ( doload ) shdr[h].sh_name = 0; /* Name is NULL. */ } if ( dsi->symtab_len == 0 ) { dsi->symtab_addr = 0; return; } if ( doload ) { *(int *)p = maxva - dsi->symtab_addr; sym_ehdr = (Elf_Ehdr *)(p + sizeof(int)); memcpy(sym_ehdr, ehdr, sizeof(Elf_Ehdr)); sym_ehdr->e_phoff = 0; sym_ehdr->e_shoff = sizeof(Elf_Ehdr); sym_ehdr->e_phentsize = 0; sym_ehdr->e_phnum = 0; sym_ehdr->e_shstrndx = SHN_UNDEF; } dsi->symtab_len = maxva - dsi->symtab_addr; dsi->v_end = maxva; } /* * Local variables: * mode: C * c-set-style: "BSD" * c-basic-offset: 4 * tab-width: 4 * indent-tabs-mode: nil * End: */ 121'>121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "ch.h"
#include "hal.h"

static virtual_timer_t vt1, vt2;

static void restart(void *p) {

  (void)p;

  chSysLockFromISR();
  uartStartSendI(&UARTD2, 14, "Hello World!\r\n");
  chSysUnlockFromISR();
}

static void ledoff(void *p) {

  (void)p;
  palSetPad(GPIOC, GPIOC_LED1);
}

/*
 * This callback is invoked when a transmission buffer has been completely
 * read by the driver.
 */
static void txend1(UARTDriver *uartp) {

  (void)uartp;
  palSetPad(GPIOC, GPIOC_LED1);
}

/*
 * This callback is invoked when a transmission has physically completed.
 */
static void txend2(UARTDriver *uartp) {

  (void)uartp;
  palSetPad(GPIOC, GPIOC_LED1);
  chSysLockFromISR();
  chVTResetI(&vt1);
  chVTDoSetI(&vt1, TIME_MS2I(5000), restart, NULL);
  chSysUnlockFromISR();
}

/*
 * This callback is invoked on a receive error, the errors mask is passed
 * as parameter.
 */
static void rxerr(UARTDriver *uartp, uartflags_t e) {

  (void)uartp;
  (void)e;
}

/*
 * This callback is invoked when a character is received but the application
 * was not ready to receive it, the character is passed as parameter.
 */
static void rxchar(UARTDriver *uartp, uint16_t c) {

  (void)uartp;
  (void)c;
  /* Flashing the LED each time a character is received.*/
  palClearPad(GPIOC, GPIOC_LED1);
  chSysLockFromISR();
  chVTResetI(&vt2);
  chVTDoSetI(&vt2, TIME_MS2I(200), ledoff, NULL);
  chSysUnlockFromISR();
}

/*
 * This callback is invoked when a receive buffer has been completely written.
 */
static void rxend(UARTDriver *uartp) {

  (void)uartp;
}

/*
 * This callback is invoked when configured timeout reached.
 */
static void rxtimeout(UARTDriver *uartp) {

  (void)uartp;
}

/*
 * UART driver configuration structure.
 */
static UARTConfig uart_cfg_1 = {
  txend1,
  txend2,
  rxend,
  rxchar,
  rxerr,
  rxtimeout,
  0,
  38400,
  0,
  USART_CR2_LINEN,
  0
};

/*
 * Application entry point.
 */
int main(void) {

  /*
   * System initializations.
   * - HAL initialization, this also initializes the configured device drivers
   *   and performs the board-specific initializations.
   * - Kernel initialization, the main() function becomes a thread and the
   *   RTOS is active.
   */
  halInit();
  chSysInit();

  /*
   * Activates the serial driver 1, PA9 and PA10 are routed to USART2.
   */
  uartStart(&UARTD2, &uart_cfg_1);

  /*
   * Starts the transmission, it will be handled entirely in background.
   */
  uartStartSend(&UARTD2, 13, "Starting...\r\n");

  /*
   * Normal main() thread activity, in this demo it does nothing.
   */
  while (true) {
    chThdSleepMilliseconds(500);
  }
}