efi_gop.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * EFI application disk support
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <efi_loader.h>
  10. #include <inttypes.h>
  11. #include <lcd.h>
  12. #include <malloc.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
  15. struct efi_gop_obj {
  16. /* Generic EFI object parent class data */
  17. struct efi_object parent;
  18. /* EFI Interface callback struct for gop */
  19. struct efi_gop ops;
  20. /* The only mode we support */
  21. struct efi_gop_mode_info info;
  22. struct efi_gop_mode mode;
  23. };
  24. static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
  25. unsigned long *size_of_info,
  26. struct efi_gop_mode_info **info)
  27. {
  28. struct efi_gop_obj *gopobj;
  29. EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
  30. gopobj = container_of(this, struct efi_gop_obj, ops);
  31. *size_of_info = sizeof(gopobj->info);
  32. *info = &gopobj->info;
  33. return EFI_EXIT(EFI_SUCCESS);
  34. }
  35. static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
  36. {
  37. EFI_ENTRY("%p, %x", this, mode_number);
  38. if (mode_number != 0)
  39. return EFI_EXIT(EFI_INVALID_PARAMETER);
  40. return EFI_EXIT(EFI_SUCCESS);
  41. }
  42. static efi_status_t EFIAPI gop_blt(struct efi_gop *this, void *buffer,
  43. unsigned long operation, unsigned long sx,
  44. unsigned long sy, unsigned long dx,
  45. unsigned long dy, unsigned long width,
  46. unsigned long height, unsigned long delta)
  47. {
  48. int i, j, line_len16, line_len32;
  49. void *fb;
  50. EFI_ENTRY("%p, %p, %lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx", this,
  51. buffer, operation, sx, sy, dx, dy, width, height, delta);
  52. if (operation != EFI_BLT_BUFFER_TO_VIDEO)
  53. return EFI_EXIT(EFI_INVALID_PARAMETER);
  54. fb = (void*)gd->fb_base;
  55. line_len16 = panel_info.vl_col * sizeof(u16);
  56. line_len32 = panel_info.vl_col * sizeof(u32);
  57. /* Copy the contents line by line */
  58. switch (panel_info.vl_bpix) {
  59. case LCD_COLOR32:
  60. for (i = 0; i < height; i++) {
  61. u32 *dest = fb + ((i + dy) * line_len32) +
  62. (dx * sizeof(u32));
  63. u32 *src = buffer + ((i + sy) * line_len32) +
  64. (sx * sizeof(u32));
  65. /* Same color format, just memcpy */
  66. memcpy(dest, src, width * sizeof(u32));
  67. }
  68. break;
  69. case LCD_COLOR16:
  70. for (i = 0; i < height; i++) {
  71. u16 *dest = fb + ((i + dy) * line_len16) +
  72. (dx * sizeof(u16));
  73. u32 *src = buffer + ((i + sy) * line_len32) +
  74. (sx * sizeof(u32));
  75. /* Convert from rgb888 to rgb565 */
  76. for (j = 0; j < width; j++) {
  77. u32 rgb888 = src[j];
  78. dest[j] = ((((rgb888 >> (16 + 3)) & 0x1f) << 11) |
  79. (((rgb888 >> (8 + 2)) & 0x3f) << 5) |
  80. (((rgb888 >> (0 + 3)) & 0x1f) << 0));
  81. }
  82. }
  83. break;
  84. }
  85. lcd_sync();
  86. return EFI_EXIT(EFI_SUCCESS);
  87. }
  88. /* This gets called from do_bootefi_exec(). */
  89. int efi_gop_register(void)
  90. {
  91. struct efi_gop_obj *gopobj;
  92. int line_len;
  93. switch (panel_info.vl_bpix) {
  94. case LCD_COLOR32:
  95. case LCD_COLOR16:
  96. break;
  97. default:
  98. /* So far, we only work in 16 or 32 bit mode */
  99. return -1;
  100. }
  101. gopobj = calloc(1, sizeof(*gopobj));
  102. /* Fill in object data */
  103. gopobj->parent.protocols[0].guid = &efi_gop_guid;
  104. gopobj->parent.protocols[0].open = efi_return_handle;
  105. gopobj->parent.handle = &gopobj->ops;
  106. gopobj->ops.query_mode = gop_query_mode;
  107. gopobj->ops.set_mode = gop_set_mode;
  108. gopobj->ops.blt = gop_blt;
  109. gopobj->ops.mode = &gopobj->mode;
  110. gopobj->mode.max_mode = 1;
  111. gopobj->mode.info = &gopobj->info;
  112. gopobj->mode.info_size = sizeof(gopobj->info);
  113. gopobj->mode.fb_base = gd->fb_base;
  114. gopobj->mode.fb_size = lcd_get_size(&line_len);
  115. gopobj->info.version = 0;
  116. gopobj->info.width = panel_info.vl_col;
  117. gopobj->info.height = panel_info.vl_row;
  118. gopobj->info.pixel_format = EFI_GOT_RGBA8;
  119. gopobj->info.pixels_per_scanline = panel_info.vl_col;
  120. /* Hook up to the device list */
  121. list_add_tail(&gopobj->parent.link, &efi_obj_list);
  122. return 0;
  123. }