1 /*- 2 * Copyright (c) 2009 Sam Leffler, Errno Consulting 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 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD$ 30 */ 31 32 /* 33 * cfi [-f device] op 34 * (default device is /dev/cfi0). 35 */ 36 #include <sys/types.h> 37 #include <sys/file.h> 38 #include <sys/ioctl.h> 39 #include <sys/cfictl.h> 40 41 #include <stdio.h> 42 #include <getopt.h> 43 #include <stdlib.h> 44 45 const char *progname; 46 const char *dvname; 47 48 static void 49 usage(void) 50 { 51 int i; 52 53 fprintf(stderr, "usage: %s [-f device] op...\n", progname); 54 fprintf(stderr, "where op's are:\n"); 55 fprintf(stderr, "fact\t\tread factory PR segment\n"); 56 fprintf(stderr, "oem\t\tread OEM segment\n"); 57 fprintf(stderr, "woem value\twrite OEM segment\n"); 58 fprintf(stderr, "plr\t\tread PLR\n"); 59 fprintf(stderr, "wplr\t\twrite PLR\n"); 60 exit(-1); 61 } 62 63 static int 64 getfd(int mode) 65 { 66 int fd = open(dvname, mode, 0); 67 if (fd < 0) 68 err(-1, "open"); 69 return fd; 70 } 71 72 static uint64_t 73 getfactorypr(void) 74 { 75 uint64_t v; 76 int fd = getfd(O_RDONLY); 77 if (ioctl(fd, CFIOCGFACTORYPR, &v) < 0) 78 err(-1, "ioctl(CFIOCGFACTORYPR)"); 79 close(fd); 80 return v; 81 } 82 83 static uint64_t 84 getoempr(void) 85 { 86 uint64_t v; 87 int fd = getfd(O_RDONLY); 88 if (ioctl(fd, CFIOCGOEMPR, &v) < 0) 89 err(-1, "ioctl(CFIOCGOEMPR)"); 90 close(fd); 91 return v; 92 } 93 94 static void 95 setoempr(uint64_t v) 96 { 97 int fd = getfd(O_WRONLY); 98 if (ioctl(fd, CFIOCSOEMPR, &v) < 0) 99 err(-1, "ioctl(CFIOCGOEMPR)"); 100 close(fd); 101 } 102 103 static uint32_t 104 getplr(void) 105 { 106 uint32_t plr; 107 int fd = getfd(O_RDONLY); 108 if (ioctl(fd, CFIOCGPLR, &plr) < 0) 109 err(-1, "ioctl(CFIOCGPLR)"); 110 close(fd); 111 return plr; 112 } 113 114 static void 115 setplr(void) 116 { 117 int fd = getfd(O_WRONLY); 118 if (ioctl(fd, CFIOCSPLR, 0) < 0) 119 err(-1, "ioctl(CFIOCPLR)"); 120 close(fd); 121 } 122 123 int 124 main(int argc, char *argv[]) 125 { 126 dvname = getenv("CFI"); 127 if (dvname == NULL) 128 dvname = "/dev/cfi0"; 129 progname = argv[0]; 130 if (argc > 1) { 131 if (strcmp(argv[1], "-f") == 0) { 132 if (argc < 2) 133 errx(1, "missing device name for -f option"); 134 dvname = argv[2]; 135 argc -= 2, argv += 2; 136 } else if (strcmp(argv[1], "-?") == 0) 137 usage(); 138 } 139 for (; argc > 1; argc--, argv++) { 140 if (strcasecmp(argv[1], "fact") == 0) { 141 printf("0x%llx\n", (unsigned long long) getfactorypr()); 142 } else if (strcasecmp(argv[1], "oem") == 0) { 143 printf("0x%llx\n", (unsigned long long) getoempr()); 144 } else if (strcasecmp(argv[1], "woem") == 0) { 145 if (argc < 2) 146 errx(1, "missing value for woem"); 147 setoempr((uint64_t) strtoull(argv[2], NULL, 0)); 148 argc--, argv++; 149 } else if (strcasecmp(argv[1], "plr") == 0) { 150 printf("0x%x\n", getplr()); 151 } else if (strcasecmp(argv[1], "wplr") == 0) { 152 setplr(); 153 } else 154 usage(); 155 } 156 } 157