選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

netdev_settings.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. assert ( bustype != NULL );
  134. strncpy ( data, bustype, len );
  135. return strlen ( bustype );
  136. }
  137. /**
  138. * Fetch bus location setting
  139. *
  140. * @v netdev Network device
  141. * @v data Buffer to fill with setting data
  142. * @v len Length of buffer
  143. * @ret len Length of setting data, or negative error
  144. */
  145. static int netdev_fetch_busloc ( struct net_device *netdev, void *data,
  146. size_t len ) {
  147. struct device_description *desc = &netdev->dev->desc;
  148. uint32_t busloc;
  149. busloc = cpu_to_be32 ( desc->location );
  150. if ( len > sizeof ( busloc ) )
  151. len = sizeof ( busloc );
  152. memcpy ( data, &busloc, len );
  153. return sizeof ( busloc );
  154. }
  155. /**
  156. * Fetch bus ID setting
  157. *
  158. * @v netdev Network device
  159. * @v data Buffer to fill with setting data
  160. * @v len Length of buffer
  161. * @ret len Length of setting data, or negative error
  162. */
  163. static int netdev_fetch_busid ( struct net_device *netdev, void *data,
  164. size_t len ) {
  165. struct device_description *desc = &netdev->dev->desc;
  166. struct dhcp_netdev_desc dhcp_desc;
  167. dhcp_desc.type = desc->bus_type;
  168. dhcp_desc.vendor = htons ( desc->vendor );
  169. dhcp_desc.device = htons ( desc->device );
  170. if ( len > sizeof ( dhcp_desc ) )
  171. len = sizeof ( dhcp_desc );
  172. memcpy ( data, &dhcp_desc, len );
  173. return sizeof ( dhcp_desc );
  174. }
  175. /**
  176. * Fetch chip setting
  177. *
  178. * @v netdev Network device
  179. * @v data Buffer to fill with setting data
  180. * @v len Length of buffer
  181. * @ret len Length of setting data, or negative error
  182. */
  183. static int netdev_fetch_chip ( struct net_device *netdev, void *data,
  184. size_t len ) {
  185. const char *chip = netdev->dev->driver_name;
  186. strncpy ( data, chip, len );
  187. return strlen ( chip );
  188. }
  189. /**
  190. * Fetch ifname setting
  191. *
  192. * @v netdev Network device
  193. * @v data Buffer to fill with setting data
  194. * @v len Length of buffer
  195. * @ret len Length of setting data, or negative error
  196. */
  197. static int netdev_fetch_ifname ( struct net_device *netdev, void *data,
  198. size_t len ) {
  199. const char *ifname = netdev->name;
  200. strncpy ( data, ifname, len );
  201. return strlen ( ifname );
  202. }
  203. /** A network device setting operation */
  204. struct netdev_setting_operation {
  205. /** Setting */
  206. const struct setting *setting;
  207. /** Store setting (or NULL if not supported)
  208. *
  209. * @v netdev Network device
  210. * @v data Setting data, or NULL to clear setting
  211. * @v len Length of setting data
  212. * @ret rc Return status code
  213. */
  214. int ( * store ) ( struct net_device *netdev, const void *data,
  215. size_t len );
  216. /** Fetch setting
  217. *
  218. * @v netdev Network device
  219. * @v data Buffer to fill with setting data
  220. * @v len Length of buffer
  221. * @ret len Length of setting data, or negative error
  222. */
  223. int ( * fetch ) ( struct net_device *netdev, void *data, size_t len );
  224. };
  225. /** Network device settings */
  226. static struct netdev_setting_operation netdev_setting_operations[] = {
  227. { &mac_setting, netdev_store_mac, netdev_fetch_mac },
  228. { &bustype_setting, NULL, netdev_fetch_bustype },
  229. { &busloc_setting, NULL, netdev_fetch_busloc },
  230. { &busid_setting, NULL, netdev_fetch_busid },
  231. { &chip_setting, NULL, netdev_fetch_chip },
  232. { &ifname_setting, NULL, netdev_fetch_ifname },
  233. };
  234. /**
  235. * Store value of network device setting
  236. *
  237. * @v settings Settings block
  238. * @v setting Setting to store
  239. * @v data Setting data, or NULL to clear setting
  240. * @v len Length of setting data
  241. * @ret rc Return status code
  242. */
  243. static int netdev_store ( struct settings *settings,
  244. const struct setting *setting,
  245. const void *data, size_t len ) {
  246. struct net_device *netdev = container_of ( settings, struct net_device,
  247. settings.settings );
  248. struct netdev_setting_operation *op;
  249. unsigned int i;
  250. /* Handle network device-specific settings */
  251. for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
  252. sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
  253. op = &netdev_setting_operations[i];
  254. if ( setting_cmp ( setting, op->setting ) == 0 ) {
  255. if ( op->store ) {
  256. return op->store ( netdev, data, len );
  257. } else {
  258. return -ENOTSUP;
  259. }
  260. }
  261. }
  262. return generic_settings_store ( settings, setting, data, len );
  263. }
  264. /**
  265. * Fetch value of network device setting
  266. *
  267. * @v settings Settings block
  268. * @v setting Setting to fetch
  269. * @v data Buffer to fill with setting data
  270. * @v len Length of buffer
  271. * @ret len Length of setting data, or negative error
  272. */
  273. static int netdev_fetch ( struct settings *settings, struct setting *setting,
  274. void *data, size_t len ) {
  275. struct net_device *netdev = container_of ( settings, struct net_device,
  276. settings.settings );
  277. struct netdev_setting_operation *op;
  278. unsigned int i;
  279. /* Handle network device-specific settings */
  280. for ( i = 0 ; i < ( sizeof ( netdev_setting_operations ) /
  281. sizeof ( netdev_setting_operations[0] ) ) ; i++ ) {
  282. op = &netdev_setting_operations[i];
  283. if ( setting_cmp ( setting, op->setting ) == 0 )
  284. return op->fetch ( netdev, data, len );
  285. }
  286. return generic_settings_fetch ( settings, setting, data, len );
  287. }
  288. /**
  289. * Clear network device settings
  290. *
  291. * @v settings Settings block
  292. */
  293. static void netdev_clear ( struct settings *settings ) {
  294. generic_settings_clear ( settings );
  295. }
  296. /** Network device configuration settings operations */
  297. struct settings_operations netdev_settings_operations = {
  298. .store = netdev_store,
  299. .fetch = netdev_fetch,
  300. .clear = netdev_clear,
  301. };
  302. /**
  303. * Redirect "netX" settings block
  304. *
  305. * @v settings Settings block
  306. * @ret settings Underlying settings block
  307. */
  308. static struct settings * netdev_redirect ( struct settings *settings ) {
  309. struct net_device *netdev;
  310. /* Redirect to most recently opened network device */
  311. netdev = last_opened_netdev();
  312. if ( netdev ) {
  313. return netdev_settings ( netdev );
  314. } else {
  315. return settings;
  316. }
  317. }
  318. /** "netX" settings operations */
  319. static struct settings_operations netdev_redirect_settings_operations = {
  320. .redirect = netdev_redirect,
  321. };
  322. /** "netX" settings */
  323. static struct settings netdev_redirect_settings = {
  324. .refcnt = NULL,
  325. .siblings = LIST_HEAD_INIT ( netdev_redirect_settings.siblings ),
  326. .children = LIST_HEAD_INIT ( netdev_redirect_settings.children ),
  327. .op = &netdev_redirect_settings_operations,
  328. };
  329. /** Initialise "netX" settings */
  330. static void netdev_redirect_settings_init ( void ) {
  331. int rc;
  332. if ( ( rc = register_settings ( &netdev_redirect_settings, NULL,
  333. "netX" ) ) != 0 ) {
  334. DBG ( "Could not register netX settings: %s\n",
  335. strerror ( rc ) );
  336. return;
  337. }
  338. }
  339. /** "netX" settings initialiser */
  340. struct init_fn netdev_redirect_settings_init_fn __init_fn ( INIT_LATE ) = {
  341. .initialise = netdev_redirect_settings_init,
  342. };