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.

pnpbios.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <realmode.h>
  22. #include <pnpbios.h>
  23. /** @file
  24. *
  25. * PnP BIOS
  26. *
  27. */
  28. /** PnP BIOS structure */
  29. struct pnp_bios {
  30. /** Signature
  31. *
  32. * Must be equal to @c PNP_BIOS_SIGNATURE
  33. */
  34. uint32_t signature;
  35. /** Version as BCD (e.g. 1.0 is 0x10) */
  36. uint8_t version;
  37. /** Length of this structure */
  38. uint8_t length;
  39. /** System capabilities */
  40. uint16_t control;
  41. /** Checksum */
  42. uint8_t checksum;
  43. } __attribute__ (( packed ));
  44. /** Signature for a PnP BIOS structure */
  45. #define PNP_BIOS_SIGNATURE \
  46. ( ( '$' << 0 ) + ( 'P' << 8 ) + ( 'n' << 16 ) + ( 'P' << 24 ) )
  47. /**
  48. * Test address for PnP BIOS structure
  49. *
  50. * @v offset Offset within BIOS segment to test
  51. * @ret rc Return status code
  52. */
  53. static int is_pnp_bios ( unsigned int offset ) {
  54. union {
  55. struct pnp_bios pnp_bios;
  56. uint8_t bytes[256]; /* 256 is maximum length possible */
  57. } u;
  58. size_t len;
  59. unsigned int i;
  60. uint8_t sum = 0;
  61. /* Read start of header and verify signature */
  62. copy_from_real ( &u.pnp_bios, BIOS_SEG, offset, sizeof ( u.pnp_bios ));
  63. if ( u.pnp_bios.signature != PNP_BIOS_SIGNATURE )
  64. return -EINVAL;
  65. /* Read whole header and verify checksum */
  66. len = u.pnp_bios.length;
  67. copy_from_real ( &u.bytes, BIOS_SEG, offset, len );
  68. for ( i = 0 ; i < len ; i++ ) {
  69. sum += u.bytes[i];
  70. }
  71. if ( sum != 0 )
  72. return -EINVAL;
  73. DBG ( "Found PnP BIOS at %04x:%04x\n", BIOS_SEG, offset );
  74. return 0;
  75. }
  76. /**
  77. * Locate Plug-and-Play BIOS
  78. *
  79. * @ret pnp_offset Offset of PnP BIOS structure within BIOS segment
  80. *
  81. * The PnP BIOS structure will be at BIOS_SEG:pnp_offset. If no PnP
  82. * BIOS is found, -1 is returned.
  83. */
  84. int find_pnp_bios ( void ) {
  85. static int pnp_offset = 0;
  86. if ( pnp_offset )
  87. return pnp_offset;
  88. for ( pnp_offset = 0 ; pnp_offset < 0x10000 ; pnp_offset += 0x10 ) {
  89. if ( is_pnp_bios ( pnp_offset ) == 0 )
  90. return pnp_offset;
  91. }
  92. pnp_offset = -1;
  93. return pnp_offset;
  94. }