1 /* 2 * Copyright 2012 Red Hat Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Ben Skeggs 23 */ 24 #include "priv.h" 25 26 #include <subdev/bios.h> 27 #include <subdev/bios/bmp.h> 28 #include <subdev/bios/bit.h> 29 30 u8 31 nvbios_checksum(const u8 *data, int size) 32 { 33 u8 sum = 0; 34 while (size--) 35 sum += *data++; 36 return sum; 37 } 38 39 u16 40 nvbios_findstr(const u8 *data, int size, const char *str, int len) 41 { 42 int i, j; 43 44 for (i = 0; i <= (size - len); i++) { 45 for (j = 0; j < len; j++) 46 if ((char)data[i + j] != str[j]) 47 break; 48 if (j == len) 49 return i; 50 } 51 52 return 0; 53 } 54 55 int 56 nvbios_memcmp(struct nvkm_bios *bios, u32 addr, const char *str, u32 len) 57 { 58 unsigned char c1, c2; 59 60 while (len--) { 61 c1 = nvbios_rd08(bios, addr++); 62 c2 = *(str++); 63 if (c1 != c2) 64 return c1 - c2; 65 } 66 return 0; 67 } 68 69 int 70 nvbios_extend(struct nvkm_bios *bios, u32 length) 71 { 72 if (bios->size < length) { 73 u8 *prev = bios->data; 74 if (!(bios->data = kmalloc(length, GFP_KERNEL))) { 75 bios->data = prev; 76 return -ENOMEM; 77 } 78 memcpy(bios->data, prev, bios->size); 79 bios->size = length; 80 kfree(prev); 81 return 1; 82 } 83 return 0; 84 } 85 86 static void * 87 nvkm_bios_dtor(struct nvkm_subdev *subdev) 88 { 89 struct nvkm_bios *bios = nvkm_bios(subdev); 90 kfree(bios->data); 91 return bios; 92 } 93 94 static const struct nvkm_subdev_func 95 nvkm_bios = { 96 .dtor = nvkm_bios_dtor, 97 }; 98 99 int 100 nvkm_bios_new(struct nvkm_device *device, int index, struct nvkm_bios **pbios) 101 { 102 struct nvkm_bios *bios; 103 struct bit_entry bit_i; 104 int ret; 105 106 if (!(bios = *pbios = kzalloc(sizeof(*bios), GFP_KERNEL))) 107 return -ENOMEM; 108 nvkm_subdev_ctor(&nvkm_bios, device, index, &bios->subdev); 109 110 ret = nvbios_shadow(bios); 111 if (ret) 112 return ret; 113 114 /* detect type of vbios we're dealing with */ 115 bios->bmp_offset = nvbios_findstr(bios->data, bios->size, 116 "\xff\x7f""NV\0", 5); 117 if (bios->bmp_offset) { 118 nvkm_debug(&bios->subdev, "BMP version %x.%x\n", 119 bmp_version(bios) >> 8, 120 bmp_version(bios) & 0xff); 121 } 122 123 bios->bit_offset = nvbios_findstr(bios->data, bios->size, 124 "\xff\xb8""BIT", 5); 125 if (bios->bit_offset) 126 nvkm_debug(&bios->subdev, "BIT signature found\n"); 127 128 /* determine the vbios version number */ 129 if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) { 130 bios->version.major = nvbios_rd08(bios, bit_i.offset + 3); 131 bios->version.chip = nvbios_rd08(bios, bit_i.offset + 2); 132 bios->version.minor = nvbios_rd08(bios, bit_i.offset + 1); 133 bios->version.micro = nvbios_rd08(bios, bit_i.offset + 0); 134 bios->version.patch = nvbios_rd08(bios, bit_i.offset + 4); 135 } else 136 if (bmp_version(bios)) { 137 bios->version.major = nvbios_rd08(bios, bios->bmp_offset + 13); 138 bios->version.chip = nvbios_rd08(bios, bios->bmp_offset + 12); 139 bios->version.minor = nvbios_rd08(bios, bios->bmp_offset + 11); 140 bios->version.micro = nvbios_rd08(bios, bios->bmp_offset + 10); 141 } 142 143 nvkm_info(&bios->subdev, "version %02x.%02x.%02x.%02x.%02x\n", 144 bios->version.major, bios->version.chip, 145 bios->version.minor, bios->version.micro, bios->version.patch); 146 return 0; 147 } 148