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.

memmap_settings.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2013 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. #include <string.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/init.h>
  28. #include <ipxe/settings.h>
  29. #include <ipxe/io.h>
  30. /** @file
  31. *
  32. * Memory map settings
  33. *
  34. * Memory map settings are numerically encoded as:
  35. *
  36. * Bits 31-24 Number of regions, minus one
  37. * Bits 23-16 Starting region
  38. * Bits 15-11 Unused
  39. * Bit 10 Ignore non-existent regions (rather than generating an error)
  40. * Bit 9 Include length
  41. * Bit 8 Include start address
  42. * Bits 7-6 Unused
  43. * Bits 5-0 Scale factor (i.e. right shift count)
  44. */
  45. /**
  46. * Construct memory map setting tag
  47. *
  48. * @v start Starting region
  49. * @v count Number of regions
  50. * @v include_start Include start address
  51. * @v include_length Include length
  52. * @v ignore Ignore non-existent regions
  53. * @v scale Scale factor
  54. * @ret tag Setting tag
  55. */
  56. #define MEMMAP_TAG( start, count, include_start, include_length, \
  57. ignore, scale ) \
  58. ( ( (start) << 16 ) | ( ( (count) - 1 ) << 24 ) | \
  59. ( (ignore) << 10 ) | ( (include_length) << 9 ) | \
  60. ( (include_start) << 8 ) | (scale) )
  61. /**
  62. * Extract number of regions from setting tag
  63. *
  64. * @v tag Setting tag
  65. * @ret count Number of regions
  66. */
  67. #define MEMMAP_COUNT( tag ) ( ( ( (tag) >> 24 ) & 0xff ) + 1 )
  68. /**
  69. * Extract starting region from setting tag
  70. *
  71. * @v tag Setting tag
  72. * @ret start Starting region
  73. */
  74. #define MEMMAP_START( tag ) ( ( (tag) >> 16 ) & 0xff )
  75. /**
  76. * Extract ignore flag from setting tag
  77. *
  78. * @v tag Setting tag
  79. * @ret ignore Ignore non-existent regions
  80. */
  81. #define MEMMAP_IGNORE_NONEXISTENT( tag ) ( (tag) & 0x00000400UL )
  82. /**
  83. * Extract length inclusion flag from setting tag
  84. *
  85. * @v tag Setting tag
  86. * @ret include_length Include length
  87. */
  88. #define MEMMAP_INCLUDE_LENGTH( tag ) ( (tag) & 0x00000200UL )
  89. /**
  90. * Extract start address inclusion flag from setting tag
  91. *
  92. * @v tag Setting tag
  93. * @ret include_start Include start address
  94. */
  95. #define MEMMAP_INCLUDE_START( tag ) ( (tag) & 0x00000100UL )
  96. /**
  97. * Extract scale factor from setting tag
  98. *
  99. * @v tag Setting tag
  100. * @v scale Scale factor
  101. */
  102. #define MEMMAP_SCALE( tag ) ( (tag) & 0x3f )
  103. /** Memory map settings scope */
  104. static const struct settings_scope memmap_settings_scope;
  105. /**
  106. * Check applicability of memory map setting
  107. *
  108. * @v settings Settings block
  109. * @v setting Setting
  110. * @ret applies Setting applies within this settings block
  111. */
  112. static int memmap_settings_applies ( struct settings *settings __unused,
  113. const struct setting *setting ) {
  114. return ( setting->scope == &memmap_settings_scope );
  115. }
  116. /**
  117. * Fetch value of memory map setting
  118. *
  119. * @v settings Settings block
  120. * @v setting Setting to fetch
  121. * @v data Buffer to fill with setting data
  122. * @v len Length of buffer
  123. * @ret len Length of setting data, or negative error
  124. */
  125. static int memmap_settings_fetch ( struct settings *settings,
  126. struct setting *setting,
  127. void *data, size_t len ) {
  128. struct memory_map memmap;
  129. struct memory_region *region;
  130. uint64_t result = 0;
  131. unsigned int start;
  132. unsigned int count;
  133. unsigned int scale;
  134. int include_start;
  135. int include_length;
  136. int ignore_nonexistent;
  137. unsigned int i;
  138. /* Parse settings tag */
  139. start = MEMMAP_START ( setting->tag );
  140. count = MEMMAP_COUNT ( setting->tag );
  141. scale = MEMMAP_SCALE ( setting->tag );
  142. include_start = MEMMAP_INCLUDE_START ( setting->tag );
  143. include_length = MEMMAP_INCLUDE_LENGTH ( setting->tag );
  144. ignore_nonexistent = MEMMAP_IGNORE_NONEXISTENT ( setting->tag );
  145. DBGC ( settings, "MEMMAP start %d count %d %s%s%s%s scale %d\n",
  146. start, count, ( include_start ? "start" : "" ),
  147. ( ( include_start && include_length ) ? "+" : "" ),
  148. ( include_length ? "length" : "" ),
  149. ( ignore_nonexistent ? " ignore" : "" ), scale );
  150. /* Fetch memory map */
  151. get_memmap ( &memmap );
  152. /* Extract results from memory map */
  153. for ( i = start ; count-- ; i++ ) {
  154. /* Check that region exists */
  155. if ( i >= memmap.count ) {
  156. if ( ignore_nonexistent ) {
  157. continue;
  158. } else {
  159. DBGC ( settings, "MEMMAP region %d does not "
  160. "exist\n", i );
  161. return -ENOENT;
  162. }
  163. }
  164. /* Extract results from this region */
  165. region = &memmap.regions[i];
  166. if ( include_start ) {
  167. result += region->start;
  168. DBGC ( settings, "MEMMAP %d start %08llx\n",
  169. i, region->start );
  170. }
  171. if ( include_length ) {
  172. result += ( region->end - region->start );
  173. DBGC ( settings, "MEMMAP %d length %08llx\n",
  174. i, ( region->end - region->start ) );
  175. }
  176. }
  177. /* Scale result */
  178. result >>= scale;
  179. /* Return result */
  180. result = cpu_to_be64 ( result );
  181. if ( len > sizeof ( result ) )
  182. len = sizeof ( result );
  183. memcpy ( data, &result, len );
  184. /* Set type if not already specified */
  185. if ( ! setting->type )
  186. setting->type = &setting_type_hexraw;
  187. return sizeof ( result );
  188. }
  189. /** Memory map settings operations */
  190. static struct settings_operations memmap_settings_operations = {
  191. .applies = memmap_settings_applies,
  192. .fetch = memmap_settings_fetch,
  193. };
  194. /** Memory map settings */
  195. static struct settings memmap_settings = {
  196. .refcnt = NULL,
  197. .siblings = LIST_HEAD_INIT ( memmap_settings.siblings ),
  198. .children = LIST_HEAD_INIT ( memmap_settings.children ),
  199. .op = &memmap_settings_operations,
  200. .default_scope = &memmap_settings_scope,
  201. };
  202. /** Initialise memory map settings */
  203. static void memmap_settings_init ( void ) {
  204. int rc;
  205. if ( ( rc = register_settings ( &memmap_settings, NULL,
  206. "memmap" ) ) != 0 ) {
  207. DBG ( "MEMMAP could not register settings: %s\n",
  208. strerror ( rc ) );
  209. return;
  210. }
  211. }
  212. /** Memory map settings initialiser */
  213. struct init_fn memmap_settings_init_fn __init_fn ( INIT_NORMAL ) = {
  214. .initialise = memmap_settings_init,
  215. };
  216. /** Memory map predefined settings */
  217. const struct setting memsize_setting __setting ( SETTING_MISC, memsize ) = {
  218. .name = "memsize",
  219. .description = "Memory size (in MB)",
  220. .tag = MEMMAP_TAG ( 0, 0x100, 0, 1, 1, 20 ),
  221. .type = &setting_type_int32,
  222. .scope = &memmap_settings_scope,
  223. };