You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acpi_settings.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2017 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * ACPI settings
  28. *
  29. */
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <ipxe/init.h>
  33. #include <ipxe/settings.h>
  34. #include <ipxe/acpi.h>
  35. /** ACPI settings scope */
  36. static const struct settings_scope acpi_settings_scope;
  37. /**
  38. * Check applicability of ACPI setting
  39. *
  40. * @v settings Settings block
  41. * @v setting Setting
  42. * @ret applies Setting applies within this settings block
  43. */
  44. static int acpi_settings_applies ( struct settings *settings __unused,
  45. const struct setting *setting ) {
  46. return ( setting->scope == &acpi_settings_scope );
  47. }
  48. /**
  49. * Fetch value of ACPI setting
  50. *
  51. * @v settings Settings block
  52. * @v setting Setting to fetch
  53. * @v data Buffer to fill with setting data
  54. * @v len Length of buffer
  55. * @ret len Length of setting data, or negative error
  56. */
  57. static int acpi_settings_fetch ( struct settings *settings,
  58. struct setting *setting,
  59. void *data, size_t len ) {
  60. struct acpi_header acpi;
  61. uint32_t tag_high;
  62. uint32_t tag_low;
  63. uint32_t tag_signature;
  64. unsigned int tag_index;
  65. size_t tag_offset;
  66. size_t tag_len;
  67. userptr_t table;
  68. size_t offset;
  69. size_t max_len;
  70. int delta;
  71. unsigned int i;
  72. /* Parse settings tag */
  73. tag_high = ( setting->tag >> 32 );
  74. tag_low = setting->tag;
  75. tag_signature = bswap_32 ( tag_high );
  76. tag_index = ( ( tag_low >> 24 ) & 0xff );
  77. tag_offset = ( ( tag_low >> 8 ) & 0xffff );
  78. tag_len = ( ( tag_low >> 0 ) & 0xff );
  79. DBGC ( settings, "ACPI %s.%d offset %#zx length %#zx\n",
  80. acpi_name ( tag_signature ), tag_index, tag_offset, tag_len );
  81. /* Locate ACPI table */
  82. table = acpi_find ( tag_signature, tag_index );
  83. if ( ! table )
  84. return -ENOENT;
  85. /* Read table header */
  86. copy_from_user ( &acpi, table, 0, sizeof ( acpi ) );
  87. /* Calculate starting offset and maximum available length */
  88. max_len = le32_to_cpu ( acpi.length );
  89. if ( tag_offset > max_len )
  90. return -ENOENT;
  91. offset = tag_offset;
  92. max_len -= offset;
  93. /* Restrict to requested length, if specified */
  94. if ( tag_len && ( tag_len < max_len ) )
  95. max_len = tag_len;
  96. /* Invert endianness for numeric settings */
  97. if ( setting->type && setting->type->numerate ) {
  98. offset += ( max_len - 1 );
  99. delta = -1;
  100. } else {
  101. delta = +1;
  102. }
  103. /* Read data */
  104. for ( i = 0 ; ( ( i < max_len ) && ( i < len ) ) ; i++ ) {
  105. copy_from_user ( data, table, offset, 1 );
  106. data++;
  107. offset += delta;
  108. }
  109. /* Set type if not already specified */
  110. if ( ! setting->type )
  111. setting->type = &setting_type_hexraw;
  112. return max_len;
  113. }
  114. /** ACPI settings operations */
  115. static struct settings_operations acpi_settings_operations = {
  116. .applies = acpi_settings_applies,
  117. .fetch = acpi_settings_fetch,
  118. };
  119. /** ACPI settings */
  120. static struct settings acpi_settings = {
  121. .refcnt = NULL,
  122. .siblings = LIST_HEAD_INIT ( acpi_settings.siblings ),
  123. .children = LIST_HEAD_INIT ( acpi_settings.children ),
  124. .op = &acpi_settings_operations,
  125. .default_scope = &acpi_settings_scope,
  126. };
  127. /** Initialise ACPI settings */
  128. static void acpi_settings_init ( void ) {
  129. int rc;
  130. if ( ( rc = register_settings ( &acpi_settings, NULL,
  131. "acpi" ) ) != 0 ) {
  132. DBG ( "ACPI could not register settings: %s\n",
  133. strerror ( rc ) );
  134. return;
  135. }
  136. }
  137. /** ACPI settings initialiser */
  138. struct init_fn acpi_settings_init_fn __init_fn ( INIT_NORMAL ) = {
  139. .initialise = acpi_settings_init,
  140. };