]> git.proxmox.com Git - grub2.git/blame - util/render-label.c
fs/ntfs: Fix various OOB reads and writes (CVE-2023-4692, CVE-2023-4693)
[grub2.git] / util / render-label.c
CommitLineData
44d1c20a
VS
1/*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
4 *
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#include <config.h>
21
22#include <grub/util/misc.h>
ee5283d6 23#include <grub/util/install.h>
44d1c20a
VS
24#include <grub/i18n.h>
25#include <grub/term.h>
26#include <grub/font.h>
27#include <grub/gfxmenu_view.h>
28#include <grub/color.h>
5177391e 29#include <grub/emu/hostdisk.h>
44d1c20a
VS
30
31#define _GNU_SOURCE 1
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
44d1c20a
VS
36#include <unistd.h>
37#include <errno.h>
38
44d1c20a
VS
39struct header
40{
41 grub_uint8_t magic;
42 grub_uint16_t width;
43 grub_uint16_t height;
7e47e27b 44} GRUB_PACKED;
44d1c20a
VS
45
46static struct grub_video_palette_data ieee1275_palette[256];
47
48void
49grub_util_render_label (const char *label_font,
50 const char *label_bgcolor,
51 const char *label_color,
52 const char *text,
53 const char *output)
54{
55 struct header head;
56 FILE *out;
57 int i, j, k, cptr = 0;
58 grub_font_t font;
59 char *fontfull;
60 const grub_uint8_t vals[] = { 0xff, 0xda, 0xb3, 0x87, 0x54, 0x00 };
61 const grub_uint8_t vals2[] = { 0xf3, 0xe7, 0xcd, 0xc0, 0xa5, 0x96,
62 0x77, 0x66, 0x3f, 0x27 };
63 int width, height;
64 grub_uint8_t bg, fg;
65 struct grub_video_mode_info mode_info;
66 grub_err_t err;
67 grub_video_rgba_color_t fgcolor;
68 grub_video_rgba_color_t bgcolor;
69
70 if (output)
71 out = grub_util_fopen (output, "wb");
72 else
73 out = stdout;
74 if (!out)
75 {
76 grub_util_error (_("cannot open `%s': %s"), output ? : "stdout",
77 strerror (errno));
78 }
79
80 if (label_color)
81 {
82 err = grub_video_parse_color (label_color, &fgcolor);
83 if (err)
84 grub_util_error (_("invalid color specification `%s'"), label_color);
85 }
86 else
87 {
88 fgcolor.red = 0x00;
89 fgcolor.green = 0x00;
90 fgcolor.blue = 0x00;
91 fgcolor.alpha = 0xff;
92 }
93
94 if (label_bgcolor)
95 {
96 err = grub_video_parse_color (label_bgcolor, &bgcolor);
97 if (err)
98 grub_util_error (_("invalid color specification `%s'"), label_bgcolor);
99 }
100 else
101 {
102 bgcolor.red = 0xff;
103 bgcolor.green = 0xff;
104 bgcolor.blue = 0xff;
105 bgcolor.alpha = 0xff;
106 }
107
108 for (i = 0; i < 256; i++)
109 ieee1275_palette[i].a = 0xff;
110
111 for (i = 0; i < 6; i++)
112 for (j = 0; j < 6; j++)
113 for (k = 0; k < 6; k++)
114 {
115 ieee1275_palette[cptr].r = vals[i];
116 ieee1275_palette[cptr].g = vals[j];
117 ieee1275_palette[cptr].b = vals[k];
118 ieee1275_palette[cptr].a = 0xff;
119 cptr++;
120 }
121 cptr--;
122 for (i = 0; i < 10; i++)
123 {
124 ieee1275_palette[cptr].r = vals2[i];
125 ieee1275_palette[cptr].g = 0;
126 ieee1275_palette[cptr].b = 0;
127 ieee1275_palette[cptr].a = 0xff;
128 cptr++;
129 }
130 for (i = 0; i < 10; i++)
131 {
132 ieee1275_palette[cptr].r = 0;
133 ieee1275_palette[cptr].g = vals2[i];
134 ieee1275_palette[cptr].b = 0;
135 ieee1275_palette[cptr].a = 0xff;
136 cptr++;
137 }
138 for (i = 0; i < 10; i++)
139 {
140 ieee1275_palette[cptr].r = 0;
141 ieee1275_palette[cptr].g = 0;
142 ieee1275_palette[cptr].b = vals2[i];
143 ieee1275_palette[cptr].a = 0xff;
144 cptr++;
145 }
146 for (i = 0; i < 10; i++)
147 {
148 ieee1275_palette[cptr].r = vals2[i];
149 ieee1275_palette[cptr].g = vals2[i];
150 ieee1275_palette[cptr].b = vals2[i];
151 ieee1275_palette[cptr].a = 0xff;
152 cptr++;
153 }
154 ieee1275_palette[cptr].r = 0;
155 ieee1275_palette[cptr].g = 0;
156 ieee1275_palette[cptr].b = 0;
157 ieee1275_palette[cptr].a = 0xff;
158
159 char * t;
27d1a67f 160 t = grub_canonicalize_file_name (label_font);
44d1c20a
VS
161 if (!t)
162 {
163 grub_util_error (_("cannot open `%s': %s"), label_font,
164 strerror (errno));
165 }
166
167 fontfull = xasprintf ("(host)/%s", t);
168 free (t);
169
44d1c20a
VS
170 grub_font_loader_init ();
171 font = grub_font_load (fontfull);
172 if (!font)
173 {
174 grub_util_error (_("cannot open `%s': %s"), label_font,
175 grub_errmsg);
176 }
177
178 width = grub_font_get_string_width (font, text) + 10;
179 height = grub_font_get_height (font);
180
181 mode_info.width = width;
182 mode_info.height = height;
183 mode_info.pitch = width;
184
185 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
186 mode_info.bpp = 8;
187 mode_info.bytes_per_pixel = 1;
188 mode_info.number_of_colors = 256;
189
190 grub_video_capture_start (&mode_info, ieee1275_palette,
191 ARRAY_SIZE (ieee1275_palette));
192
193 fg = grub_video_map_rgb (fgcolor.red,
194 fgcolor.green,
195 fgcolor.blue);
196 bg = grub_video_map_rgb (bgcolor.red,
197 bgcolor.green,
198 bgcolor.blue);
199
200 grub_memset (grub_video_capture_get_framebuffer (), bg, height * width);
201 grub_font_draw_string (text, font, fg,
202 5, grub_font_get_ascent (font));
203
204 head.magic = 1;
205 head.width = grub_cpu_to_be16 (width);
206 head.height = grub_cpu_to_be16 (height);
207 fwrite (&head, 1, sizeof (head), out);
208 fwrite (grub_video_capture_get_framebuffer (), 1, width * height, out);
209
210 grub_video_capture_end ();
211 if (out != stdout)
212 fclose (out);
213
214 free (fontfull);
215}