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 3.6KB

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