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.

netdev_settings.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (C) 2008 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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <string.h>
  20. #include <errno.h>
  21. #include <byteswap.h>
  22. #include <ipxe/dhcp.h>
  23. #include <ipxe/dhcpopts.h>
  24. #include <ipxe/settings.h>
  25. #include <ipxe/device.h>
  26. #include <ipxe/netdevice.h>
  27. /** @file
  28. *
  29. * Network device configuration settings
  30. *
  31. */
  32. /** Network device named settings */
  33. struct setting mac_setting __setting ( SETTING_NETDEV ) = {
  34. .name = "mac",
  35. .description = "MAC address",
  36. .type = &setting_type_hex,
  37. .tag = NETDEV_SETTING_TAG_MAC,
  38. };
  39. struct setting busid_setting __setting ( SETTING_NETDEV ) = {
  40. .name = "busid",
  41. .description = "Bus ID",
  42. .type = &setting_type_hex,
  43. .tag = NETDEV_SETTING_TAG_BUS_ID,
  44. };
  45. /**
  46. * Check applicability of network device setting
  47. *
  48. * @v settings Settings block
  49. * @v setting Setting
  50. * @ret applies Setting applies within this settings block
  51. */
  52. static int netdev_applies ( struct settings *settings __unused,
  53. struct setting *setting ) {
  54. return ( IS_NETDEV_SETTING_TAG ( setting->tag ) ||
  55. dhcpopt_applies ( setting->tag ) );
  56. }
  57. /**
  58. * Store value of network device setting
  59. *
  60. * @v settings Settings block
  61. * @v setting Setting to store
  62. * @v data Setting data, or NULL to clear setting
  63. * @v len Length of setting data
  64. * @ret rc Return status code
  65. */
  66. static int netdev_store ( struct settings *settings, struct setting *setting,
  67. const void *data, size_t len ) {
  68. struct net_device *netdev = container_of ( settings, struct net_device,
  69. settings.settings );
  70. if ( setting_cmp ( setting, &mac_setting ) == 0 ) {
  71. if ( len != netdev->ll_protocol->ll_addr_len )
  72. return -EINVAL;
  73. memcpy ( netdev->ll_addr, data, len );
  74. return 0;
  75. }
  76. if ( setting_cmp ( setting, &busid_setting ) == 0 )
  77. return -ENOTSUP;
  78. return generic_settings_store ( settings, setting, data, len );
  79. }
  80. /**
  81. * Fetch value of network device setting
  82. *
  83. * @v settings Settings block
  84. * @v setting Setting to fetch
  85. * @v data Setting data, or NULL to clear setting
  86. * @v len Length of setting data
  87. * @ret rc Return status code
  88. */
  89. static int netdev_fetch ( struct settings *settings, struct setting *setting,
  90. void *data, size_t len ) {
  91. struct net_device *netdev = container_of ( settings, struct net_device,
  92. settings.settings );
  93. struct device_description *desc = &netdev->dev->desc;
  94. struct dhcp_netdev_desc dhcp_desc;
  95. if ( setting_cmp ( setting, &mac_setting ) == 0 ) {
  96. if ( len > netdev->ll_protocol->ll_addr_len )
  97. len = netdev->ll_protocol->ll_addr_len;
  98. memcpy ( data, netdev->ll_addr, len );
  99. return netdev->ll_protocol->ll_addr_len;
  100. }
  101. if ( setting_cmp ( setting, &busid_setting ) == 0 ) {
  102. dhcp_desc.type = desc->bus_type;
  103. dhcp_desc.vendor = htons ( desc->vendor );
  104. dhcp_desc.device = htons ( desc->device );
  105. if ( len > sizeof ( dhcp_desc ) )
  106. len = sizeof ( dhcp_desc );
  107. memcpy ( data, &dhcp_desc, len );
  108. return sizeof ( dhcp_desc );
  109. }
  110. return generic_settings_fetch ( settings, setting, data, len );
  111. }
  112. /**
  113. * Clear network device settings
  114. *
  115. * @v settings Settings block
  116. */
  117. static void netdev_clear ( struct settings *settings ) {
  118. generic_settings_clear ( settings );
  119. }
  120. /** Network device configuration settings operations */
  121. struct settings_operations netdev_settings_operations = {
  122. .applies = netdev_applies,
  123. .store = netdev_store,
  124. .fetch = netdev_fetch,
  125. .clear = netdev_clear,
  126. };