VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   55 Tags
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   1) /* Write the contents of the <certfile> into kernel symbol system_extra_cert
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   2)  *
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   3)  * Copyright (C) IBM Corporation, 2015
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   4)  *
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   5)  * Author: Mehmet Kayaalp <mkayaalp@linux.vnet.ibm.com>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   6)  *
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   7)  * This software may be used and distributed according to the terms
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   8)  * of the GNU General Public License, incorporated herein by reference.
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500   9)  *
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  10)  * Usage: insert-sys-cert [-s <System.map> -b <vmlinux> -c <certfile>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  11)  */
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  12) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  13) #define _GNU_SOURCE
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  14) #include <stdio.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  15) #include <ctype.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  16) #include <string.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  17) #include <limits.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  18) #include <stdbool.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  19) #include <errno.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  20) #include <stdlib.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  21) #include <stdarg.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  22) #include <sys/types.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  23) #include <sys/stat.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  24) #include <sys/mman.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  25) #include <fcntl.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  26) #include <unistd.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  27) #include <elf.h>
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  28) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  29) #define CERT_SYM  "system_extra_cert"
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  30) #define USED_SYM  "system_extra_cert_used"
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  31) #define LSIZE_SYM "system_certificate_list_size"
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  32) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  33) #define info(format, args...) fprintf(stderr, "INFO:    " format, ## args)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  34) #define warn(format, args...) fprintf(stdout, "WARNING: " format, ## args)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  35) #define  err(format, args...) fprintf(stderr, "ERROR:   " format, ## args)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  36) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  37) #if UINTPTR_MAX == 0xffffffff
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  38) #define CURRENT_ELFCLASS ELFCLASS32
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  39) #define Elf_Ehdr	Elf32_Ehdr
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  40) #define Elf_Shdr	Elf32_Shdr
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  41) #define Elf_Sym		Elf32_Sym
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  42) #else
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  43) #define CURRENT_ELFCLASS ELFCLASS64
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  44) #define Elf_Ehdr	Elf64_Ehdr
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  45) #define Elf_Shdr	Elf64_Shdr
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  46) #define Elf_Sym		Elf64_Sym
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  47) #endif
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  48) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  49) static unsigned char endianness(void)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  50) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  51) 	uint16_t two_byte = 0x00FF;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  52) 	uint8_t low_address = *((uint8_t *)&two_byte);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  53) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  54) 	if (low_address == 0)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  55) 		return ELFDATA2MSB;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  56) 	else
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  57) 		return ELFDATA2LSB;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  58) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  59) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  60) struct sym {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  61) 	char *name;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  62) 	unsigned long address;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  63) 	unsigned long offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  64) 	void *content;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  65) 	int size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  66) };
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  67) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  68) static unsigned long get_offset_from_address(Elf_Ehdr *hdr, unsigned long addr)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  69) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  70) 	Elf_Shdr *x;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  71) 	unsigned int i, num_sections;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  72) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  73) 	x = (void *)hdr + hdr->e_shoff;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  74) 	if (hdr->e_shnum == SHN_UNDEF)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  75) 		num_sections = x[0].sh_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  76) 	else
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  77) 		num_sections = hdr->e_shnum;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  78) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  79) 	for (i = 1; i < num_sections; i++) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  80) 		unsigned long start = x[i].sh_addr;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  81) 		unsigned long end = start + x[i].sh_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  82) 		unsigned long offset = x[i].sh_offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  83) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  84) 		if (addr >= start && addr <= end)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  85) 			return addr - start + offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  86) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  87) 	return 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  88) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  89) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  90) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  91) #define LINE_SIZE 100
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  92) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  93) static void get_symbol_from_map(Elf_Ehdr *hdr, FILE *f, char *name,
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  94) 				struct sym *s)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  95) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  96) 	char l[LINE_SIZE];
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  97) 	char *w, *p, *n;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  98) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500  99) 	s->size = 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 100) 	s->address = 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 101) 	s->offset = 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 102) 	if (fseek(f, 0, SEEK_SET) != 0) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 103) 		perror("File seek failed");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 104) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 105) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 106) 	while (fgets(l, LINE_SIZE, f)) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 107) 		p = strchr(l, '\n');
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 108) 		if (!p) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 109) 			err("Missing line ending.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 110) 			return;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 111) 		}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 112) 		n = strstr(l, name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 113) 		if (n)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 114) 			break;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 115) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 116) 	if (!n) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 117) 		err("Unable to find symbol: %s\n", name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 118) 		return;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 119) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 120) 	w = strchr(l, ' ');
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 121) 	if (!w)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 122) 		return;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 123) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 124) 	*w = '\0';
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 125) 	s->address = strtoul(l, NULL, 16);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 126) 	if (s->address == 0)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 127) 		return;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 128) 	s->offset = get_offset_from_address(hdr, s->address);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 129) 	s->name = name;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 130) 	s->content = (void *)hdr + s->offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 131) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 132) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 133) static Elf_Sym *find_elf_symbol(Elf_Ehdr *hdr, Elf_Shdr *symtab, char *name)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 134) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 135) 	Elf_Sym *sym, *symtab_start;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 136) 	char *strtab, *symname;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 137) 	unsigned int link;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 138) 	Elf_Shdr *x;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 139) 	int i, n;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 140) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 141) 	x = (void *)hdr + hdr->e_shoff;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 142) 	link = symtab->sh_link;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 143) 	symtab_start = (void *)hdr + symtab->sh_offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 144) 	n = symtab->sh_size / symtab->sh_entsize;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 145) 	strtab = (void *)hdr + x[link].sh_offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 146) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 147) 	for (i = 0; i < n; i++) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 148) 		sym = &symtab_start[i];
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 149) 		symname = strtab + sym->st_name;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 150) 		if (strcmp(symname, name) == 0)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 151) 			return sym;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 152) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 153) 	err("Unable to find symbol: %s\n", name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 154) 	return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 155) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 156) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 157) static void get_symbol_from_table(Elf_Ehdr *hdr, Elf_Shdr *symtab,
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 158) 				  char *name, struct sym *s)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 159) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 160) 	Elf_Shdr *sec;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 161) 	int secndx;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 162) 	Elf_Sym *elf_sym;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 163) 	Elf_Shdr *x;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 164) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 165) 	x = (void *)hdr + hdr->e_shoff;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 166) 	s->size = 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 167) 	s->address = 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 168) 	s->offset = 0;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 169) 	elf_sym = find_elf_symbol(hdr, symtab, name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 170) 	if (!elf_sym)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 171) 		return;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 172) 	secndx = elf_sym->st_shndx;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 173) 	if (!secndx)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 174) 		return;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 175) 	sec = &x[secndx];
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 176) 	s->size = elf_sym->st_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 177) 	s->address = elf_sym->st_value;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 178) 	s->offset = s->address - sec->sh_addr
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 179) 			       + sec->sh_offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 180) 	s->name = name;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 181) 	s->content = (void *)hdr + s->offset;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 182) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 183) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 184) static Elf_Shdr *get_symbol_table(Elf_Ehdr *hdr)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 185) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 186) 	Elf_Shdr *x;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 187) 	unsigned int i, num_sections;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 188) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 189) 	x = (void *)hdr + hdr->e_shoff;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 190) 	if (hdr->e_shnum == SHN_UNDEF)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 191) 		num_sections = x[0].sh_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 192) 	else
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 193) 		num_sections = hdr->e_shnum;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 194) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 195) 	for (i = 1; i < num_sections; i++)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 196) 		if (x[i].sh_type == SHT_SYMTAB)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 197) 			return &x[i];
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 198) 	return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 199) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 200) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 201) static void *map_file(char *file_name, int *size)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 202) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 203) 	struct stat st;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 204) 	void *map;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 205) 	int fd;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 206) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 207) 	fd = open(file_name, O_RDWR);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 208) 	if (fd < 0) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 209) 		perror(file_name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 210) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 211) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 212) 	if (fstat(fd, &st)) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 213) 		perror("Could not determine file size");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 214) 		close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 215) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 216) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 217) 	*size = st.st_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 218) 	map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 219) 	if (map == MAP_FAILED) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 220) 		perror("Mapping to memory failed");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 221) 		close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 222) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 223) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 224) 	close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 225) 	return map;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 226) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 227) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 228) static char *read_file(char *file_name, int *size)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 229) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 230) 	struct stat st;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 231) 	char *buf;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 232) 	int fd;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 233) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 234) 	fd = open(file_name, O_RDONLY);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 235) 	if (fd < 0) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 236) 		perror(file_name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 237) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 238) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 239) 	if (fstat(fd, &st)) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 240) 		perror("Could not determine file size");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 241) 		close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 242) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 243) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 244) 	*size = st.st_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 245) 	buf = malloc(*size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 246) 	if (!buf) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 247) 		perror("Allocating memory failed");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 248) 		close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 249) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 250) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 251) 	if (read(fd, buf, *size) != *size) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 252) 		perror("File read failed");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 253) 		close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 254) 		return NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 255) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 256) 	close(fd);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 257) 	return buf;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 258) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 259) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 260) static void print_sym(Elf_Ehdr *hdr, struct sym *s)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 261) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 262) 	info("sym:    %s\n", s->name);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 263) 	info("addr:   0x%lx\n", s->address);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 264) 	info("size:   %d\n", s->size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 265) 	info("offset: 0x%lx\n", (unsigned long)s->offset);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 266) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 267) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 268) static void print_usage(char *e)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 269) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 270) 	printf("Usage %s [-s <System.map>] -b <vmlinux> -c <certfile>\n", e);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 271) }
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 272) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 273) int main(int argc, char **argv)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 274) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 275) 	char *system_map_file = NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 276) 	char *vmlinux_file = NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 277) 	char *cert_file = NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 278) 	int vmlinux_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 279) 	int cert_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 280) 	Elf_Ehdr *hdr;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 281) 	char *cert;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 282) 	FILE *system_map;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 283) 	unsigned long *lsize;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 284) 	int *used;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 285) 	int opt;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 286) 	Elf_Shdr *symtab = NULL;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 287) 	struct sym cert_sym, lsize_sym, used_sym;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 288) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 289) 	while ((opt = getopt(argc, argv, "b:c:s:")) != -1) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 290) 		switch (opt) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 291) 		case 's':
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 292) 			system_map_file = optarg;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 293) 			break;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 294) 		case 'b':
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 295) 			vmlinux_file = optarg;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 296) 			break;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 297) 		case 'c':
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 298) 			cert_file = optarg;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 299) 			break;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 300) 		default:
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 301) 			break;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 302) 		}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 303) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 304) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 305) 	if (!vmlinux_file || !cert_file) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 306) 		print_usage(argv[0]);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 307) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 308) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 309) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 310) 	cert = read_file(cert_file, &cert_size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 311) 	if (!cert)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 312) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 313) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 314) 	hdr = map_file(vmlinux_file, &vmlinux_size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 315) 	if (!hdr)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 316) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 317) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 318) 	if (vmlinux_size < sizeof(*hdr)) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 319) 		err("Invalid ELF file.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 320) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 321) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 322) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 323) 	if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 324) 	    (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 325) 	    (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 326) 	    (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 327) 		err("Invalid ELF magic.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 328) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 329) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 330) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 331) 	if (hdr->e_ident[EI_CLASS] != CURRENT_ELFCLASS) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 332) 		err("ELF class mismatch.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 333) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 334) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 335) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 336) 	if (hdr->e_ident[EI_DATA] != endianness()) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 337) 		err("ELF endian mismatch.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 338) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 339) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 340) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 341) 	if (hdr->e_shoff > vmlinux_size) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 342) 		err("Could not find section header.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 343) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 344) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 345) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 346) 	symtab = get_symbol_table(hdr);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 347) 	if (!symtab) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 348) 		warn("Could not find the symbol table.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 349) 		if (!system_map_file) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 350) 			err("Please provide a System.map file.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 351) 			print_usage(argv[0]);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 352) 			exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 353) 		}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 354) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 355) 		system_map = fopen(system_map_file, "r");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 356) 		if (!system_map) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 357) 			perror(system_map_file);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 358) 			exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 359) 		}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 360) 		get_symbol_from_map(hdr, system_map, CERT_SYM, &cert_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 361) 		get_symbol_from_map(hdr, system_map, USED_SYM, &used_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 362) 		get_symbol_from_map(hdr, system_map, LSIZE_SYM, &lsize_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 363) 		cert_sym.size = used_sym.address - cert_sym.address;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 364) 	} else {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 365) 		info("Symbol table found.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 366) 		if (system_map_file)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 367) 			warn("System.map is ignored.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 368) 		get_symbol_from_table(hdr, symtab, CERT_SYM, &cert_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 369) 		get_symbol_from_table(hdr, symtab, USED_SYM, &used_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 370) 		get_symbol_from_table(hdr, symtab, LSIZE_SYM, &lsize_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 371) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 372) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 373) 	if (!cert_sym.offset || !lsize_sym.offset || !used_sym.offset)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 374) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 375) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 376) 	print_sym(hdr, &cert_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 377) 	print_sym(hdr, &used_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 378) 	print_sym(hdr, &lsize_sym);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 379) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 380) 	lsize = (unsigned long *)lsize_sym.content;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 381) 	used = (int *)used_sym.content;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 382) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 383) 	if (cert_sym.size < cert_size) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 384) 		err("Certificate is larger than the reserved area!\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 385) 		exit(EXIT_FAILURE);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 386) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 387) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 388) 	/* If the existing cert is the same, don't overwrite */
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 389) 	if (cert_size == *used &&
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 390) 	    strncmp(cert_sym.content, cert, cert_size) == 0) {
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 391) 		warn("Certificate was already inserted.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 392) 		exit(EXIT_SUCCESS);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 393) 	}
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 394) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 395) 	if (*used > 0)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 396) 		warn("Replacing previously inserted certificate.\n");
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 397) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 398) 	memcpy(cert_sym.content, cert, cert_size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 399) 	if (cert_size < cert_sym.size)
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 400) 		memset(cert_sym.content + cert_size,
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 401) 			0, cert_sym.size - cert_size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 402) 
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 403) 	*lsize = *lsize + cert_size - *used;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 404) 	*used = cert_size;
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 405) 	info("Inserted the contents of %s into %lx.\n", cert_file,
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 406) 						cert_sym.address);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 407) 	info("Used %d bytes out of %d bytes reserved.\n", *used,
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 408) 						 cert_sym.size);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 409) 	exit(EXIT_SUCCESS);
c4c3610595857 (Mehmet Kayaalp 2015-11-24 16:18:05 -0500 410) }