コード例 #1
0
ファイル: plugin.c プロジェクト: Winter3un/ctf_task
static int
try_load_plugin (const char *pname, bfd *abfd, int *has_plugin_p)
{
  void *plugin_handle;
  struct ld_plugin_tv tv[4];
  int i;
  ld_plugin_onload onload;
  enum ld_plugin_status status;

  *has_plugin_p = 0;

  plugin_handle = dlopen (pname, RTLD_NOW);
  if (!plugin_handle)
    {
      _bfd_error_handler ("%s\n", dlerror ());
      return 0;
    }

  onload = dlsym (plugin_handle, "onload");
  if (!onload)
    goto err;

  i = 0;
  tv[i].tv_tag = LDPT_MESSAGE;
  tv[i].tv_u.tv_message = message;

  ++i;
  tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
  tv[i].tv_u.tv_register_claim_file = register_claim_file;

  ++i;
  tv[i].tv_tag = LDPT_ADD_SYMBOLS;
  tv[i].tv_u.tv_add_symbols = add_symbols;

  ++i;
  tv[i].tv_tag = LDPT_NULL;
  tv[i].tv_u.tv_val = 0;

  status = (*onload)(tv);

  if (status != LDPS_OK)
    goto err;

  *has_plugin_p = 1;

  abfd->plugin_format = bfd_plugin_no;

  if (!claim_file)
    goto err;

  if (!try_claim (abfd))
    goto err;

  abfd->plugin_format = bfd_plugin_yes;

  return 1;

 err:
  return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: kzlin129/tt-gpl
void start(unsigned long a1, unsigned long a2, void *promptr)
{
    unsigned long i;
    kernel_entry_t kernel_entry;
    Elf64_Ehdr *elf64;
    Elf64_Phdr *elf64ph;

    prom = (int (*)(void *)) promptr;
    chosen_handle = finddevice("/chosen");
    if (chosen_handle == (void *) -1)
        exit();
    if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
        exit();
    stderr = stdout;
    if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4)
        exit();

    printf("\n\rzImage starting: loaded at 0x%x\n\r", (unsigned)_start);

    /*
     * Now we try to claim some memory for the kernel itself
     * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what
     * our Makefile stuffs in is an image containing all sort of junk including
     * an ELF header. We need to do some calculations here to find the right
     * size... In practice we add 1Mb, that is enough, but we should really
     * consider fixing the Makefile to put a _raw_ kernel in there !
     */
    vmlinux_memsize += 0x100000;
    printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize);
    vmlinux.addr = try_claim(vmlinux_memsize);
    if (vmlinux.addr == 0) {
        printf("Can't allocate memory for kernel image !\n\r");
        exit();
    }
    vmlinuz.addr = (unsigned long)_vmlinux_start;
    vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);
    vmlinux.size = PAGE_ALIGN(vmlinux_filesize);
    vmlinux.memsize = vmlinux_memsize;

    /*
     * Now we try to claim memory for the initrd (and copy it there)
     */
    initrd.size = (unsigned long)(_initrd_end - _initrd_start);
    initrd.memsize = initrd.size;
    if ( initrd.size > 0 ) {
        printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
        initrd.addr = try_claim(initrd.size);
        if (initrd.addr == 0) {
            printf("Can't allocate memory for initial ramdisk !\n\r");
            exit();
        }
        a1 = initrd.addr;
        a2 = initrd.size;
        printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
               initrd.addr, (unsigned long)_initrd_start, initrd.size);
        memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
        printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
    }

    /* Eventually gunzip the kernel */
    if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
        int len;
        avail_ram = scratch;
        begin_avail = avail_high = avail_ram;
        end_avail = scratch + sizeof(scratch);
        printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
               vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
        len = vmlinuz.size;
        gunzip((void *)vmlinux.addr, vmlinux.size,
               (unsigned char *)vmlinuz.addr, &len);
        printf("done 0x%lx bytes\n\r", len);
        printf("0x%x bytes of heap consumed, max in use 0x%x\n\r",
               (unsigned)(avail_high - begin_avail), heap_max);
    } else {
        memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
    }

    /* Skip over the ELF header */
    elf64 = (Elf64_Ehdr *)vmlinux.addr;
    if ( elf64->e_ident[EI_MAG0]  != ELFMAG0	||
            elf64->e_ident[EI_MAG1]  != ELFMAG1	||
            elf64->e_ident[EI_MAG2]  != ELFMAG2	||
            elf64->e_ident[EI_MAG3]  != ELFMAG3	||
            elf64->e_ident[EI_CLASS] != ELFCLASS64	||
            elf64->e_ident[EI_DATA]  != ELFDATA2MSB	||
            elf64->e_type            != ET_EXEC	||
            elf64->e_machine         != EM_PPC64 )
    {
        printf("Error: not a valid PPC64 ELF file!\n\r");
        exit();
    }

    elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
                             (unsigned long)elf64->e_phoff);
    for(i=0; i < (unsigned int)elf64->e_phnum ; i++,elf64ph++) {
        if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0)
            break;
    }
#ifdef DEBUG
    printf("... skipping 0x%lx bytes of ELF header\n\r",
           (unsigned long)elf64ph->p_offset);
#endif
    vmlinux.addr += (unsigned long)elf64ph->p_offset;
    vmlinux.size -= (unsigned long)elf64ph->p_offset;

    flush_cache((void *)vmlinux.addr, vmlinux.size);

    kernel_entry = (kernel_entry_t)vmlinux.addr;
#ifdef DEBUG
    printf( "kernel:\n\r"
            "        entry addr = 0x%lx\n\r"
            "        a1         = 0x%lx,\n\r"
            "        a2         = 0x%lx,\n\r"
            "        prom       = 0x%lx,\n\r"
            "        bi_recs    = 0x%lx,\n\r",
            (unsigned long)kernel_entry, a1, a2,
            (unsigned long)prom, NULL);
#endif

    kernel_entry( a1, a2, prom, NULL );

    printf("Error: Linux kernel returned to zImage bootloader!\n\r");

    exit();
}
コード例 #3
0
ファイル: main.c プロジェクト: FatSunHYS/OSCourseDesign
void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
{
	int len;
	kernel_entry_t kernel_entry;

	memset(__bss_start, 0, _end - __bss_start);

	prom = (int (*)(void *)) promptr;
	chosen_handle = finddevice("/chosen");
	if (chosen_handle == (void *) -1)
		exit();
	if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4)
		exit();

	printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", _start, sp);

	/*
	 * The first available claim_base must be above the end of the
	 * the loaded kernel wrapper file (_start to _end includes the
	 * initrd image if it is present) and rounded up to a nice
	 * 1 MB boundary for good measure.
	 */

	claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB);

	vmlinuz.addr = (unsigned long)_vmlinux_start;
	vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start);

	/* gunzip the ELF header of the kernel */
	if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
		len = vmlinuz.size;
		gunzip(elfheader, sizeof(elfheader),
				(unsigned char *)vmlinuz.addr, &len);
	} else
		memcpy(elfheader, (const void *)vmlinuz.addr, sizeof(elfheader));

	if (!is_elf64(elfheader) && !is_elf32(elfheader)) {
		printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r");
		exit();
	}

	/* We need to claim the memsize plus the file offset since gzip
	 * will expand the header (file offset), then the kernel, then
	 * possible rubbish we don't care about. But the kernel bss must
	 * be claimed (it will be zero'd by the kernel itself)
	 */
	printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
	vmlinux.addr = try_claim(vmlinux.memsize);
	if (vmlinux.addr == 0) {
		printf("Can't allocate memory for kernel image !\n\r");
		exit();
	}

	/*
	 * Now we try to claim memory for the initrd (and copy it there)
	 */
	initrd.size = (unsigned long)(_initrd_end - _initrd_start);
	initrd.memsize = initrd.size;
	if ( initrd.size > 0 ) {
		printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size);
		initrd.addr = try_claim(initrd.size);
		if (initrd.addr == 0) {
			printf("Can't allocate memory for initial ramdisk !\n\r");
			exit();
		}
		a1 = initrd.addr;
		a2 = initrd.size;
		printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r",
		       initrd.addr, (unsigned long)_initrd_start, initrd.size);
		memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size);
		printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr));
	}

	/* Eventually gunzip the kernel */
	if (*(unsigned short *)vmlinuz.addr == 0x1f8b) {
		printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...",
		       vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size);
		len = vmlinuz.size;
		gunzip((void *)vmlinux.addr, vmlinux.memsize,
			(unsigned char *)vmlinuz.addr, &len);
		printf("done 0x%lx bytes\n\r", len);
	} else {
		memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size);
	}

	export_cmdline(chosen_handle);

	/* Skip over the ELF header */
#ifdef DEBUG
	printf("... skipping 0x%lx bytes of ELF header\n\r",
			elfoffset);
#endif
	vmlinux.addr += elfoffset;

	flush_cache((void *)vmlinux.addr, vmlinux.size);

	kernel_entry = (kernel_entry_t)vmlinux.addr;
#ifdef DEBUG
	printf( "kernel:\n\r"
		"        entry addr = 0x%lx\n\r"
		"        a1         = 0x%lx,\n\r"
		"        a2         = 0x%lx,\n\r"
		"        prom       = 0x%lx,\n\r"
		"        bi_recs    = 0x%lx,\n\r",
		(unsigned long)kernel_entry, a1, a2,
		(unsigned long)prom, NULL);
#endif

	kernel_entry(a1, a2, prom, NULL);

	printf("Error: Linux kernel returned to zImage bootloader!\n\r");

	exit();
}