Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

netdev_settings.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. * 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/dhcp.h>
  28. #include <ipxe/dhcpopts.h>
  29. #include <ipxe/settings.h>
  30. #include <ipxe/device.h>
  31. #include <ipxe/netdevice.h>
  32. #include <ipxe/init.h>
  33. /** @file
  34. *
  35. * Network device configuration settings
  36. *
  37. */
  38. /** Network device predefined settings */
  39. const struct setting mac_setting __setting ( SETTING_NETDEV, mac ) = {
  40. .name = "mac",
  41. .description = "MAC address",
  42. .type = &setting_type_hex,
  43. };
  44. const struct setting bustype_setting __setting ( SETTING_NETDEV, bustype ) = {
  45. .name = "bustype",
  46. .description = "Bus type",
  47. .type = &setting_type_string,
  48. };
  49. const struct setting busloc_setting __setting ( SETTING_NETDEV, busloc ) = {
  50. .name = "busloc",
  51. .description = "Bus location",
  52. .type = &setting_type_uint32,
  53. };
  54. const struct setting busid_setting __setting ( SETTING_NETDEV, busid ) = {
  55. .name = "busid",
  56. .description = "Bus ID",
  57. .type = &setting_type_hex,
  58. };
  59. const struct setting chip_setting __setting ( SETTING_NETDEV, chip ) = {
  60. .name = "chip",
  61. .description = "Chip",
  62. .type = &setting_type_string,
  63. };
  64. const struct setting ifname_setting __setting ( SETTING_NETDEV, ifname ) = {
  65. .name = "ifname",
  66. .description = "Interface name",
  67. .type = &setting_type_string,
  68. };
  69. /**
  70. * Store MAC address setting
  71. *
  72. * @v netdev Network device
  73. * @v data Setting data, or NULL to clear setting
  74. * @v len Length of setting data
  75. * @ret rc Return status code
  76. */
  77. static int netdev_store_mac ( struct net_device *netdev,
  78. const void *data, size_t len ) {
  79. struct ll_protocol *ll_protocol = netdev->ll_protocol;
  80. /* Record new MAC address */
  81. if ( data ) {
  82. if ( len != netdev->ll_protocol->ll_addr_len )
  83. return -EINVAL;
  84. memcpy ( netdev->ll_addr, data, len );
  85. } else {
  86. /* Reset MAC address if clearing setting */
  87. ll_protocol->init_addr ( netdev->hw_addr, netdev->ll_addr );
  88. }
  89. return 0;
  90. }
  91. /**
  92. * Fetch MAC address setting
  93. *
  94. * @v netdev Network device
  95. * @v data Buffer to fill with setting data
  96. * @v len Length of buffer
  97. * @ret len Length of setting data, or negative error
  98. */
  99. static int netdev_fetch_mac ( struct net_device *netdev, void *data,
  100. size_t len ) {
  101. if ( len > netdev->ll_protocol->ll_addr_len )
  102. len = netdev->ll_protocol->ll_addr_len;
  103. memcpy ( data, netdev->ll_addr, len );
  104. return netdev->ll_protocol->ll_addr_len;
  105. }
  106. /**
  107. * Fetch bus type setting
  108. *
  109. * @v netdev Network device
  110. * @v data Buffer to fill with setting data
  111. * @v len Length of buffer
  112. * @ret len Length of setting data, or negative error
  113. */
  114. static int netdev_fetch_bustype ( struct net_device *netdev, void *data,
  115. size_t len ) {
  116. static const char *bustypes[] = {
  117. [BUS_TYPE_PCI] = "PCI",
  118. [BUS_TYPE_ISAPNP] = "ISAPNP",
  119. [BUS_TYPE_EISA] = "EISA",
  120. [BUS_TYPE_MCA] = "MCA",
  121. [BUS_TYPE_ISA] = "ISA",
  122. [BUS_TYPE_TAP] = "TAP",
  123. [BUS_TYPE_EFI] = "EFI",
  124. [BUS_TYPE_XEN] = "XEN",
  125. [BUS_TYPE_HV] = "HV",
  126. [BUS_TYPE_USB] = "USB",
  127. };
  128. struct device_description *desc = &netdev->dev->desc;
  129. const char *bustype;
  130. assert ( desc->bus_type < ( sizeof ( bustypes ) /
  131. sizeof ( bustypes[0] ) ) );
  132. bustype = bustypes[desc->bus_type];
  133. if ( ! bustype )
  134. return -ENOENT;
  135. strncpy ( data, bustype, len );
  136. return strlen ( bustype );
  137. }
  138. /**
  139. * Fetch bus location setting
  140. *
  141. * @v netdev Network device
  142. * @v data Buffer to fill with setting data
  143. * @v len Length of buffer
  144. * @ret len Length of setting data, or negative error
  145. */
  146. static int netdev_fetch_busloc ( struct net_device *netdev, void *data,
  147. size_t len ) {
  148. struct device_description *desc = &netdev->dev->desc;
  149. uint32_t busloc;
  150. busloc = cpu_to_be32 ( desc->location );
  151. if ( len > sizeof ( busloc ) )
  152. len = sizeof ( busloc );
  153. memcpy ( data, &busloc, len );
  154. return sizeof ( busloc );
  155. }
  156. /**
  157. * Fetch bus ID setting
  158. *
  159. * @v netdev Network device
  160. * @v data Buffer to fill with setting data
  161. * @v len Length of buffer
  162. * @ret len Length of setting data, or negative error
  163. */
  164. static int netdev_fetch_busid ( struct net_device *netdev, void *data,
  165. size_t len ) {
  166. struct device_description *desc = &netdev->dev->desc;
  167. struct dhcp_netdev_desc dhcp_desc;
  168. dhcp_desc.type = desc->bus_type;
  169. dhcp_desc.vendor = htons ( desc->vendor );
  170. dhcp_desc.device = htons ( desc->device );
  171. if ( len > sizeof ( dhcp_desc ) )
  172. len = sizeof ( dhcp_desc );
  173. memcpy ( data, &dhcp_desc, len );
  174. return sizeof ( dhcp_desc );
  175. }
  176. /**
  177. * Fetch chip setting
  178. *
  179. * @v netdev Network device
  180. * @v data Buffer to fill with setting data
  181. * @v len Length of buffer
  182. * @ret len Length of setting data, or negative error
  183. */
  184. static int netdev_fetch_chip ( struct net_device *netdev, void *data,
  185. size_t len ) {
  186. const char *chip = netdev->dev->driver_name;
  187. strncpy ( data, chip, len );
  188. return strlen ( chip );
  189. }
  190. /**
  191. * Fetch ifname setting
  192. *
  193. * @v netdev Network device
  194. * @v data Buffer to fill with setting data
  195. * @v len Length of buffer
  196. * @ret len Length of setting data, or negative error
  197. */
  198. static int netdev_fetch_ifname ( struct net_device *netdev, void *data,
  199. size_t len ) {
  200. const char *ifname = netdev->name;
  201. strncpy ( data, ifname, len );
  202. return strlen ( ifname );
  203. }
  204. /** A network device setting operation */
  205. struct netdev_setting_operation {
  206. /** Setting */
  207. const struct setting *setting;
  208. /** Store setting (or NULL if not supported)
  209. *
  210. * @v netdev Network device
  211. * @v data Setting data, or NULL to clear setting
  212. * @v len Length of setting data
  213. * @ret rc Return status code
  214. */
  215. int ( * store ) ( struct net_device *netdev, const void *data,
  216. size_t len );
  217. /** Fetch setting
  218. *
  219. * @v netdev Network device
  220. * @v data Buffer to fill with setting data
  221. * @v len Length of buffer
  222. * @ret len Length of setting data, or negative error
  223. */
  224. int ( * fetch ) ( struct net_device *netdev, void *data, size_t len );
  225. };
  226. /** Network device settings */
  227. static struct netdev_setting_operation netdev_setting_operations[] = {
  228. { &mac_setting, netdev_store_mac, netdev_fetch_mac },
  229. { &bustype_setting, NULL, netdev_fetch_bustype },
  230. { &busloc_setting, NULL, netdev_fetch_busloc },
  231. { &busid_setting, NULL, netdev_fetch_busid },
  232. { &chip_setting, NULL, netdev_fetch_chip },
  233. { &ifname_setting, NULL, netdev_fetch_ifname },
  234. };
  235. /**
  236. * Store value of network device setting
  237. *
  238. * @v settings Settings block
  239. * @v setting Setting to store
  240. * @v data Setting data, or NULL to clear setting
  241. * @v len Length of setting data
  242. * @ret rc Return status code
  243. */
  244. static int netdev_store ( struct settings *settings,
  245. const struct setting *setting,
  246. const void *data, size_t len ) {
  247. struct net_device *netdev = container_of ( settings, struct net_device,
  248. settings.settings );
  249. struct netdev_setting_operation *op;
  250. unsigned int i;
  251. /* Handle network device-specific settings */
  252. for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
  253. sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
  254. op = &netdev_setting_operations[i];
  255. if ( setting_cmp ( setting, op->setting ) == 0 ) {
  256. if ( op->store ) {
  257. return op->store ( netdev, data, len );
  258. } else {
  259. return -ENOTSUP;
  260. }
  261. }
  262. }
  263. return generic_settings_store ( settings, setting, data, len );
  264. }
  265. /**
  266. * Fetch value of network device setting
  267. *
  268. * @v settings Settings block
  269. * @v setting Setting to fetch
  270. * @v data Buffer to fill with setting data
  271. * @v len Length of buffer
  272. * @ret len Length of setting data, or negative error
  273. */
  274. static int netdev_fetch ( struct settings *settings, struct setting *setting,
  275. void *data, size_t len ) {
  276. struct net_device *netdev = container_of ( settings, struct net_device,
  277. settings.settings );
  278. struct netdev_setting_operation *op;
  279. unsigned int i;
  280. /* Handle network device-specific settings */
  281. for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
  282. sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
  283. op = &netdev_setting_operations[i];
  284. if ( setting_cmp ( setting, op->setting ) == 0 )
  285. return op->fetch ( netdev, data, len );
  286. }
  287. return generic_settings_fetch ( settings, setting, data, len );
  288. }
  289. /**
  290. * Clear network device settings
  291. *
  292. * @v settings Settings block
  293. */
  294. static void netdev_clear ( struct settings *settings ) {
  295. generic_settings_clear ( settings );
  296. }
  297. /** Network device configuration settings operations */
  298. struct settings_operations netdev_settings_operations = {
  299. .store = netdev_store,
  300. .fetch = netdev_fetch,
  301. .clear = netdev_clear,
  302. };
  303. /**
  304. * Redirect "netX" settings block
  305. *
  306. * @v settings Settings block
  307. * @ret settings Underlying settings block
  308. */
  309. static struct settings * netdev_redirect ( struct settings *settings ) {
  310. struct net_device *netdev;
  311. /* Redirect to most recently opened network device */
  312. netdev = last_opened_netdev();
  313. if ( netdev ) {
  314. return netdev_settings ( netdev );
  315. } else {
  316. return settings;
  317. }
  318. }
  319. /** "netX" settings operations */
  320. static struct settings_operations netdev_redirect_settings_operations = {
  321. .redirect = netdev_redirect,
  322. };
  323. /** "netX" settings */
  324. static struct settings netdev_redirect_settings = {
  325. .refcnt = NULL,
  326. .siblings = LIST_HEAD_INIT ( netdev_redirect_settings.siblings ),
  327. .children = LIST_HEAD_INIT ( netdev_redirect_settings.children ),
  328. .op = &netdev_redirect_settings_operations,
  329. };
  330. /** Initialise "netX" settings */
  331. static void netdev_redirect_settings_init ( void ) {
  332. int rc;
  333. if ( ( rc = register_settings ( &netdev_redirect_settings, NULL,
  334. "netX" ) ) != 0 ) {
  335. DBG ( "Could not register netX settings: %s\n",
  336. strerror ( rc ) );
  337. return;
  338. }
  339. }
  340. /** "netX" settings initialiser */
  341. struct init_fn netdev_redirect_settings_init_fn __init_fn ( INIT_LATE ) = {
  342. .initialise = netdev_redirect_settings_init,
  343. };