1 /*- 2 * Copyright (c) 1996 S�ren Schmidt 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software withough specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <stdlib.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <unistd.h> 35 #include <fcntl.h> 36 #include <sys/imgact_elf.h> 37 38 int usage(void); 39 40 int 41 main(int argc, char **argv) 42 { 43 44 const char *type = "FreeBSD"; 45 int retval = 0; 46 int ch, change = 0, verbose = 0; 47 48 while ((ch = getopt(argc, argv, "t:v")) != EOF) 49 switch (ch) { 50 case 'v': 51 verbose = 1; 52 break; 53 case 't': 54 change = 1; 55 type = optarg; 56 break; 57 default: 58 usage(); 59 } 60 argc -= optind; 61 argv += optind; 62 if (!argc) { 63 fprintf(stderr, "No file(s) specified.\n"); 64 exit(1); 65 } 66 while (argc) { 67 int fd; 68 char buffer[EI_NINDENT]; 69 char string[(EI_NINDENT-EI_SPARE)+1]; 70 71 if ((fd = open(argv[0], O_RDWR, 0)) < 0) { 72 fprintf(stderr, "No such file %s.\n", argv[0]); 73 retval = 1; 74 goto fail; 75 76 } 77 if (read(fd, buffer, EI_NINDENT) < EI_NINDENT) { 78 fprintf(stderr, "File '%s' too short.\n", argv[0]); 79 retval = 1; 80 goto fail; 81 } 82 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 || 83 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) { 84 fprintf(stderr, "File '%s' is not ELF format.\n", 85 argv[0]); 86 retval = 1; 87 goto fail; 88 } 89 if (!change) { 90 bzero(string, sizeof(string)); 91 strncpy(string, &buffer[EI_SPARE], EI_NINDENT-EI_SPARE); 92 if (strlen(string)) { 93 fprintf(stdout, "File '%s' is of brand '%s'.\n", 94 argv[0], string); 95 } 96 else 97 fprintf(stdout, "File '%s' has no branding.\n", 98 argv[0]); 99 } 100 else { 101 strncpy(&buffer[EI_SPARE], type, EI_NINDENT-EI_SPARE); 102 lseek(fd, 0, SEEK_SET); 103 if (write(fd, buffer, EI_NINDENT) != EI_NINDENT) { 104 fprintf(stderr, "Error writing %s\n", argv[0]); 105 retval = 1; 106 goto fail; 107 } 108 } 109 fail: 110 argc--; 111 argv++; 112 } 113 114 return retval; 115 } 116 117 int 118 usage(void) 119 { 120 fprintf(stderr, "Usage: brandelf [-t string] file ...\n"); 121 exit(1); 122 } 123