]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/bios/base.c
drm/nouveau/bios: guard against out-of-bounds accesses to image
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / bios / base.c
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 static bool
31 nvbios_addr(struct nvkm_bios *bios, u32 *addr, u8 size)
32 {
33         if (unlikely(*addr + size >= bios->size)) {
34                 nvkm_error(&bios->subdev, "OOB %d %08x\n", size, *addr);
35                 return false;
36         }
37         return true;
38 }
39
40 u8
41 nvbios_rd08(struct nvkm_bios *bios, u32 addr)
42 {
43         if (likely(nvbios_addr(bios, &addr, 1)))
44                 return bios->data[addr];
45         return 0x00;
46 }
47
48 u16
49 nvbios_rd16(struct nvkm_bios *bios, u32 addr)
50 {
51         if (likely(nvbios_addr(bios, &addr, 2)))
52                 return get_unaligned_le16(&bios->data[addr]);
53         return 0x0000;
54 }
55
56 u32
57 nvbios_rd32(struct nvkm_bios *bios, u32 addr)
58 {
59         if (likely(nvbios_addr(bios, &addr, 4)))
60                 return get_unaligned_le32(&bios->data[addr]);
61         return 0x00000000;
62 }
63
64 u8
65 nvbios_checksum(const u8 *data, int size)
66 {
67         u8 sum = 0;
68         while (size--)
69                 sum += *data++;
70         return sum;
71 }
72
73 u16
74 nvbios_findstr(const u8 *data, int size, const char *str, int len)
75 {
76         int i, j;
77
78         for (i = 0; i <= (size - len); i++) {
79                 for (j = 0; j < len; j++)
80                         if ((char)data[i + j] != str[j])
81                                 break;
82                 if (j == len)
83                         return i;
84         }
85
86         return 0;
87 }
88
89 int
90 nvbios_memcmp(struct nvkm_bios *bios, u32 addr, const char *str, u32 len)
91 {
92         unsigned char c1, c2;
93
94         while (len--) {
95                 c1 = nvbios_rd08(bios, addr++);
96                 c2 = *(str++);
97                 if (c1 != c2)
98                         return c1 - c2;
99         }
100         return 0;
101 }
102
103 int
104 nvbios_extend(struct nvkm_bios *bios, u32 length)
105 {
106         if (bios->size < length) {
107                 u8 *prev = bios->data;
108                 if (!(bios->data = kmalloc(length, GFP_KERNEL))) {
109                         bios->data = prev;
110                         return -ENOMEM;
111                 }
112                 memcpy(bios->data, prev, bios->size);
113                 bios->size = length;
114                 kfree(prev);
115                 return 1;
116         }
117         return 0;
118 }
119
120 static void *
121 nvkm_bios_dtor(struct nvkm_subdev *subdev)
122 {
123         struct nvkm_bios *bios = nvkm_bios(subdev);
124         kfree(bios->data);
125         return bios;
126 }
127
128 static const struct nvkm_subdev_func
129 nvkm_bios = {
130         .dtor = nvkm_bios_dtor,
131 };
132
133 int
134 nvkm_bios_new(struct nvkm_device *device, int index, struct nvkm_bios **pbios)
135 {
136         struct nvkm_bios *bios;
137         struct bit_entry bit_i;
138         int ret;
139
140         if (!(bios = *pbios = kzalloc(sizeof(*bios), GFP_KERNEL)))
141                 return -ENOMEM;
142         nvkm_subdev_ctor(&nvkm_bios, device, index, &bios->subdev);
143
144         ret = nvbios_shadow(bios);
145         if (ret)
146                 return ret;
147
148         /* detect type of vbios we're dealing with */
149         bios->bmp_offset = nvbios_findstr(bios->data, bios->size,
150                                           "\xff\x7f""NV\0", 5);
151         if (bios->bmp_offset) {
152                 nvkm_debug(&bios->subdev, "BMP version %x.%x\n",
153                            bmp_version(bios) >> 8,
154                            bmp_version(bios) & 0xff);
155         }
156
157         bios->bit_offset = nvbios_findstr(bios->data, bios->size,
158                                           "\xff\xb8""BIT", 5);
159         if (bios->bit_offset)
160                 nvkm_debug(&bios->subdev, "BIT signature found\n");
161
162         /* determine the vbios version number */
163         if (!bit_entry(bios, 'i', &bit_i) && bit_i.length >= 4) {
164                 bios->version.major = nvbios_rd08(bios, bit_i.offset + 3);
165                 bios->version.chip  = nvbios_rd08(bios, bit_i.offset + 2);
166                 bios->version.minor = nvbios_rd08(bios, bit_i.offset + 1);
167                 bios->version.micro = nvbios_rd08(bios, bit_i.offset + 0);
168                 bios->version.patch = nvbios_rd08(bios, bit_i.offset + 4);
169         } else
170         if (bmp_version(bios)) {
171                 bios->version.major = nvbios_rd08(bios, bios->bmp_offset + 13);
172                 bios->version.chip  = nvbios_rd08(bios, bios->bmp_offset + 12);
173                 bios->version.minor = nvbios_rd08(bios, bios->bmp_offset + 11);
174                 bios->version.micro = nvbios_rd08(bios, bios->bmp_offset + 10);
175         }
176
177         nvkm_info(&bios->subdev, "version %02x.%02x.%02x.%02x.%02x\n",
178                   bios->version.major, bios->version.chip,
179                   bios->version.minor, bios->version.micro, bios->version.patch);
180         return 0;
181 }