1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
/*
* Copyright (C) ST-Ericsson AB 2010
*
* Author: Johan Mossberg <johan.xx.mossberg@stericsson.com> for ST-Ericsson
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "b2r2_internal.h"
#include "b2r2_input_validation.h"
#include "b2r2_debug.h"
#include "b2r2_utils.h"
#include <video/b2r2_blt.h>
#include <linux/kernel.h>
#include <linux/errno.h>
static bool is_valid_format(enum b2r2_blt_fmt fmt);
static bool is_valid_bg_format(enum b2r2_blt_fmt fmt);
static bool is_valid_pitch_for_fmt(struct device *dev,
u32 pitch, s32 width, enum b2r2_blt_fmt fmt);
static bool is_aligned_width_for_fmt(s32 width, enum b2r2_blt_fmt fmt);
static s32 width_2_complete_width(s32 width, enum b2r2_blt_fmt fmt);
static bool is_complete_width_for_fmt(s32 width, enum b2r2_blt_fmt fmt);
static bool is_valid_height_for_fmt(s32 height, enum b2r2_blt_fmt fmt);
static bool validate_img(struct device *dev,
struct b2r2_blt_img *img);
static bool validate_rect(struct device *dev,
struct b2r2_blt_rect *rect);
static bool is_valid_format(enum b2r2_blt_fmt fmt)
{
switch (fmt) {
case B2R2_BLT_FMT_1_BIT_A1:
case B2R2_BLT_FMT_8_BIT_A8:
case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
case B2R2_BLT_FMT_16_BIT_ARGB4444:
case B2R2_BLT_FMT_16_BIT_ARGB1555:
case B2R2_BLT_FMT_16_BIT_RGB565:
case B2R2_BLT_FMT_Y_CB_Y_CR:
case B2R2_BLT_FMT_CB_Y_CR_Y:
case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
case B2R2_BLT_FMT_24_BIT_RGB888:
case B2R2_BLT_FMT_24_BIT_ARGB8565:
case B2R2_BLT_FMT_24_BIT_YUV888:
case B2R2_BLT_FMT_32_BIT_ARGB8888:
case B2R2_BLT_FMT_32_BIT_ABGR8888:
case B2R2_BLT_FMT_32_BIT_AYUV8888:
case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
case B2R2_BLT_FMT_24_BIT_VUY888:
case B2R2_BLT_FMT_32_BIT_VUYA8888:
return true;
default:
return false;
}
}
static bool is_valid_bg_format(enum b2r2_blt_fmt fmt)
{
switch (fmt) {
case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV444_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
return false;
default:
return true;
}
}
static bool is_valid_pitch_for_fmt(struct device *dev,
u32 pitch, s32 width, enum b2r2_blt_fmt fmt)
{
s32 complete_width;
u32 pitch_derived_from_width;
complete_width = width_2_complete_width(width, fmt);
pitch_derived_from_width = b2r2_calc_pitch_from_width(dev,
complete_width, fmt);
if (pitch < pitch_derived_from_width)
return false;
switch (fmt) {
case B2R2_BLT_FMT_16_BIT_ARGB4444:
case B2R2_BLT_FMT_16_BIT_ARGB1555:
case B2R2_BLT_FMT_16_BIT_RGB565:
if (!b2r2_is_aligned(pitch, 2))
return false;
break;
case B2R2_BLT_FMT_Y_CB_Y_CR:
case B2R2_BLT_FMT_CB_Y_CR_Y:
case B2R2_BLT_FMT_24_BIT_RGB888:
case B2R2_BLT_FMT_24_BIT_ARGB8565:
case B2R2_BLT_FMT_24_BIT_YUV888:
case B2R2_BLT_FMT_24_BIT_VUY888:
case B2R2_BLT_FMT_32_BIT_ARGB8888:
case B2R2_BLT_FMT_32_BIT_ABGR8888:
case B2R2_BLT_FMT_32_BIT_AYUV8888:
case B2R2_BLT_FMT_32_BIT_VUYA8888:
if (!b2r2_is_aligned(pitch, 4))
return false;
break;
default:
break;
}
return true;
}
static bool is_aligned_width_for_fmt(s32 width, enum b2r2_blt_fmt fmt)
{
switch (fmt) {
case B2R2_BLT_FMT_24_BIT_RGB888:
case B2R2_BLT_FMT_24_BIT_ARGB8565:
case B2R2_BLT_FMT_24_BIT_YUV888:
case B2R2_BLT_FMT_24_BIT_VUY888:
if (!b2r2_is_aligned(width, 4))
return false;
break;
case B2R2_BLT_FMT_1_BIT_A1:
if (!b2r2_is_aligned(width, 8))
return false;
break;
case B2R2_BLT_FMT_Y_CB_Y_CR:
case B2R2_BLT_FMT_CB_Y_CR_Y:
if (!b2r2_is_aligned(width, 2))
return false;
break;
default:
break;
}
return true;
}
static s32 width_2_complete_width(s32 width, enum b2r2_blt_fmt fmt)
{
switch (fmt) {
case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_Y_CB_Y_CR:
case B2R2_BLT_FMT_CB_Y_CR_Y:
case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
return b2r2_align_up(width, 2);
case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
return b2r2_align_up(width, 16);
default:
return width;
}
}
static bool is_complete_width_for_fmt(s32 width, enum b2r2_blt_fmt fmt)
{
switch (fmt) {
case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_Y_CB_Y_CR:
case B2R2_BLT_FMT_CB_Y_CR_Y:
case B2R2_BLT_FMT_YUV422_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV422_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU422_PACKED_SEMI_PLANAR:
if (!b2r2_is_aligned(width, 2))
return false;
break;
case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
if (!b2r2_is_aligned(width, 16))
return false;
break;
default:
break;
}
return true;
}
static bool is_valid_height_for_fmt(s32 height, enum b2r2_blt_fmt fmt)
{
switch (fmt) {
case B2R2_BLT_FMT_YUV420_PACKED_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_PLANAR:
case B2R2_BLT_FMT_YUV420_PACKED_SEMI_PLANAR:
case B2R2_BLT_FMT_YVU420_PACKED_SEMI_PLANAR:
if (!b2r2_is_aligned(height, 2))
return false;
break;
case B2R2_BLT_FMT_YUV420_PACKED_SEMIPLANAR_MB_STE:
case B2R2_BLT_FMT_YUV422_PACKED_SEMIPLANAR_MB_STE:
if (!b2r2_is_aligned(height, 16))
return false;
break;
default:
break;
}
return true;
}
static bool validate_img(struct device *dev,
struct b2r2_blt_img *img)
{
/*
* So that we always can do width * height * bpp without overflowing a
* 32 bit signed integer. isqrt(s32_max / max_bpp) was used to
* calculate the value.
*/
static const s32 max_img_width_height = 8191;
s32 img_size;
if (!is_valid_format(img->fmt)) {
b2r2_log_info(dev, "Validation Error: "
"!is_valid_format(img->fmt)\n");
return false;
}
if (img->width < 0 || img->width > max_img_width_height ||
img->height < 0 || img->height > max_img_width_height) {
b2r2_log_info(dev, "Validation Error: "
"img->width < 0 || "
"img->width > max_img_width_height || "
"img->height < 0 || "
"img->height > max_img_width_height\n");
return false;
}
if (b2r2_is_mb_fmt(img->fmt)) {
if (!is_complete_width_for_fmt(img->width, img->fmt)) {
b2r2_log_info(dev, "Validation Error: "
"!is_complete_width_for_fmt(img->width,"
" img->fmt)\n");
return false;
}
} else {
if (0 == img->pitch &&
(!is_aligned_width_for_fmt(img->width, img->fmt) ||
!is_complete_width_for_fmt(img->width, img->fmt))) {
b2r2_log_info(dev,
"Validation Error: "
"0 == img->pitch && "
"(!is_aligned_width_for_fmt(img->width,"
" img->fmt) || "
"!is_complete_width_for_fmt(img->width,"
" img->fmt))\n");
return false;
}
if (img->pitch != 0 &&
!is_valid_pitch_for_fmt(dev, img->pitch, img->width,
img->fmt)) {
b2r2_log_info(dev,
"Validation Error: "
"img->pitch != 0 && "
"!is_valid_pitch_for_fmt(dev, "
"img->pitch, img->width, img->fmt)\n");
return false;
}
}
if (!is_valid_height_for_fmt(img->width, img->fmt)) {
b2r2_log_info(dev, "Validation Error: "
"!is_valid_height_for_fmt(img->width, img->fmt)\n");
return false;
}
img_size = b2r2_get_img_size(dev, img);
/*
* To keep the entire image inside s32 range.
*/
if ((B2R2_BLT_PTR_HWMEM_BUF_NAME_OFFSET == img->buf.type ||
B2R2_BLT_PTR_FD_OFFSET == img->buf.type) &&
img->buf.offset > (u32)b2r2_s32_max - (u32)img_size) {
b2r2_log_info(dev, "Validation Error: "
"(B2R2_BLT_PTR_HWMEM_BUF_NAME_OFFSET == "
"img->buf.type || B2R2_BLT_PTR_FD_OFFSET == "
"img->buf.type) && img->buf.offset > "
"(u32)B2R2_MAX_S32 - (u32)img_size\n");
return false;
}
return true;
}
static bool validate_rect(struct device *dev,
struct b2r2_blt_rect *rect)
{
if (rect->width < 0 || rect->height < 0) {
b2r2_log_info(dev, "Validation Error: "
"rect->width < 0 || rect->height < 0\n");
return false;
}
return true;
}
bool b2r2_validate_user_req(struct device *dev,
struct b2r2_blt_req *req)
{
bool is_src_img_used;
bool is_bg_img_used;
bool is_src_mask_used;
bool is_dst_clip_rect_used;
if (req->size != sizeof(struct b2r2_blt_req)) {
b2r2_log_err(dev, "Validation Error: "
"req->size != sizeof(struct b2r2_blt_req)\n");
return false;
}
is_src_img_used = !(req->flags & B2R2_BLT_FLAG_SOURCE_FILL ||
req->flags & B2R2_BLT_FLAG_SOURCE_FILL_RAW);
is_bg_img_used = (req->flags & B2R2_BLT_FLAG_BG_BLEND);
is_src_mask_used = req->flags & B2R2_BLT_FLAG_SOURCE_MASK;
is_dst_clip_rect_used = req->flags & B2R2_BLT_FLAG_DESTINATION_CLIP;
if (is_src_img_used || is_src_mask_used) {
if (!validate_rect(dev, &req->src_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_rect(dev, &req->src_rect)\n");
return false;
}
}
if (!validate_rect(dev, &req->dst_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_rect(dev, &req->dst_rect)\n");
return false;
}
if (is_bg_img_used) {
if (!validate_rect(dev, &req->bg_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_rect(dev, &req->bg_rect)\n");
return false;
}
}
if (is_dst_clip_rect_used) {
if (!validate_rect(dev, &req->dst_clip_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_rect(dev, &req->dst_clip_rect)\n");
return false;
}
}
if (is_src_img_used) {
struct b2r2_blt_rect src_img_bounding_rect;
if (!validate_img(dev, &req->src_img)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_img(dev, &req->src_img)\n");
return false;
}
b2r2_get_img_bounding_rect(&req->src_img,
&src_img_bounding_rect);
if (!b2r2_is_rect_inside_rect(&req->src_rect,
&src_img_bounding_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!b2r2_is_rect_inside_rect(&req->src_rect, "
"&src_img_bounding_rect)\n");
return false;
}
}
if (is_bg_img_used) {
struct b2r2_blt_rect bg_img_bounding_rect;
if (!validate_img(dev, &req->bg_img)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_img(dev, &req->bg_img)\n");
return false;
}
if (!is_valid_bg_format(req->bg_img.fmt)) {
b2r2_log_info(dev, "Validation Error: "
"!is_valid_bg_format(req->bg_img->fmt)\n");
return false;
}
b2r2_get_img_bounding_rect(&req->bg_img,
&bg_img_bounding_rect);
if (!b2r2_is_rect_inside_rect(&req->bg_rect,
&bg_img_bounding_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!b2r2_is_rect_inside_rect(&req->bg_rect, "
"&bg_img_bounding_rect)\n");
return false;
}
}
if (is_src_mask_used) {
struct b2r2_blt_rect src_mask_bounding_rect;
if (!validate_img(dev, &req->src_mask)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_img(dev, &req->src_mask)\n");
return false;
}
b2r2_get_img_bounding_rect(&req->src_mask,
&src_mask_bounding_rect);
if (!b2r2_is_rect_inside_rect(&req->src_rect,
&src_mask_bounding_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!b2r2_is_rect_inside_rect(&req->src_rect, "
"&src_mask_bounding_rect)\n");
return false;
}
}
if (!validate_img(dev, &req->dst_img)) {
b2r2_log_info(dev, "Validation Error: "
"!validate_img(dev, &req->dst_img)\n");
return false;
}
if (is_bg_img_used) {
if (!b2r2_is_rect_gte_rect(&req->bg_rect, &req->dst_rect)) {
b2r2_log_info(dev, "Validation Error: "
"!b2r2_is_rect_gte_rect(&req->bg_rect, "
"&req->dst_rect)\n");
return false;
}
}
return true;
}
|