4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Commmon routines, handling iscsi boot props
31 #include <sys/types.h>
33 #include <sys/bootprops.h>
34 #include <sys/cmn_err.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
39 extern void *memset(void *s
, int c
, size_t n
);
40 extern int memcmp(const void *s1
, const void *s2
, size_t n
);
41 extern void bcopy(const void *s1
, void *s2
, size_t n
);
42 extern size_t strlen(const char *s
);
43 static void kinet_ntoa(char *buf
, void *in
, int af
);
44 extern ib_boot_prop_t
*iscsiboot_prop
;
46 int iscsi_print_bootprop
= 0;
48 #define ISCSI_BOOTPROP_BUFLEN 256
50 static int replace_sp_c(unsigned char *dst
, unsigned char *source
, size_t n
);
53 iscsi_bootprop_print(int level
, char *str
)
58 if (iscsi_print_bootprop
== 1) {
59 cmn_err(level
, "%s", str
);
64 iscsi_print_initiator_property(ib_ini_prop_t
*ibinitp
)
66 char outbuf
[ISCSI_BOOTPROP_BUFLEN
] = {0};
68 if (ibinitp
== NULL
) {
72 if (ibinitp
->ini_name
!= NULL
) {
73 (void) sprintf(outbuf
,
74 "Initiator Name : %s\n",
76 iscsi_bootprop_print(CE_CONT
, outbuf
);
79 if (ibinitp
->ini_chap_name
!= NULL
) {
80 (void) memset(outbuf
, 0, ISCSI_BOOTPROP_BUFLEN
);
81 (void) sprintf(outbuf
,
82 "Initiator CHAP Name : %s\n",
83 ibinitp
->ini_chap_name
);
85 iscsi_bootprop_print(CE_CONT
, outbuf
);
90 iscsi_print_nic_property(ib_nic_prop_t
*nicp
)
92 char outbuf
[ISCSI_BOOTPROP_BUFLEN
] = {0};
93 char ipaddr
[50] = {0};
100 kinet_ntoa(ipaddr
, &nicp
->nic_ip_u
, nicp
->sin_family
);
101 n
= snprintf(outbuf
, ISCSI_BOOTPROP_BUFLEN
,
102 "Local IP addr : %s\n", ipaddr
);
104 (void) memset(ipaddr
, 0, 50);
105 kinet_ntoa(ipaddr
, &nicp
->nic_gw_u
, nicp
->sin_family
);
106 n
= n
+ snprintf(outbuf
+ n
, ISCSI_BOOTPROP_BUFLEN
- n
,
107 "Local gateway : %s\n", ipaddr
);
109 (void) memset(ipaddr
, 0, 50);
110 kinet_ntoa(ipaddr
, &nicp
->nic_dhcp_u
, nicp
->sin_family
);
111 n
= n
+ snprintf(outbuf
+ n
, ISCSI_BOOTPROP_BUFLEN
- n
,
112 "Local DHCP : %s\n", ipaddr
);
114 (void) snprintf(outbuf
+ n
, ISCSI_BOOTPROP_BUFLEN
- n
,
115 "Local MAC : %02x:%02x:%02x:%02x:%02x:%02x\n",
123 iscsi_bootprop_print(CE_CONT
, outbuf
);
127 iscsi_print_tgt_property(ib_tgt_prop_t
*itgtp
)
129 char outbuf
[ISCSI_BOOTPROP_BUFLEN
] = {0};
130 char ipaddr
[50] = {0};
136 if (itgtp
->tgt_name
!= NULL
) {
137 (void) memset(outbuf
, 0, ISCSI_BOOTPROP_BUFLEN
);
138 (void) sprintf(outbuf
,
139 "Target Name : %s\n",
141 iscsi_bootprop_print(CE_CONT
, outbuf
);
144 kinet_ntoa(ipaddr
, &itgtp
->tgt_ip_u
, itgtp
->sin_family
);
145 (void) sprintf(outbuf
,
148 "Boot LUN : %02x%02x-%02x%02x-%02x%02x-%02x%02x\n",
151 itgtp
->tgt_boot_lun
[0],
152 itgtp
->tgt_boot_lun
[1],
153 itgtp
->tgt_boot_lun
[2],
154 itgtp
->tgt_boot_lun
[3],
155 itgtp
->tgt_boot_lun
[4],
156 itgtp
->tgt_boot_lun
[5],
157 itgtp
->tgt_boot_lun
[6],
158 itgtp
->tgt_boot_lun
[7]);
159 iscsi_bootprop_print(CE_CONT
, outbuf
);
161 if (itgtp
->tgt_chap_name
!= NULL
) {
162 (void) memset(outbuf
, 0, ISCSI_BOOTPROP_BUFLEN
);
163 (void) sprintf(outbuf
,
165 itgtp
->tgt_chap_name
);
166 iscsi_bootprop_print(CE_CONT
, outbuf
);
171 iscsi_print_boot_property()
173 if (iscsiboot_prop
== NULL
) {
177 iscsi_print_initiator_property(
178 &iscsiboot_prop
->boot_init
);
180 iscsi_print_nic_property(&iscsiboot_prop
->boot_nic
);
182 iscsi_print_tgt_property(&iscsiboot_prop
->boot_tgt
);
186 iscsi_boot_free_ini(ib_ini_prop_t
*init
)
192 if (init
->ini_name
!= NULL
) {
193 kmem_free(init
->ini_name
, init
->ini_name_len
);
194 init
->ini_name
= NULL
;
195 init
->ini_name_len
= 0;
197 if (init
->ini_chap_name
!= NULL
) {
198 kmem_free(init
->ini_chap_name
,
199 init
->ini_chap_name_len
);
200 init
->ini_chap_name
= NULL
;
201 init
->ini_chap_name_len
= 0;
203 if (init
->ini_chap_sec
!= NULL
) {
204 kmem_free(init
->ini_chap_sec
,
205 init
->ini_chap_sec_len
);
206 init
->ini_chap_sec
= NULL
;
207 init
->ini_chap_sec_len
= 0;
212 iscsi_boot_free_tgt(ib_tgt_prop_t
*target
)
214 if (target
== NULL
) {
218 if (target
->tgt_name
!= NULL
) {
219 kmem_free(target
->tgt_name
,
220 target
->tgt_name_len
);
221 target
->tgt_name
= NULL
;
222 target
->tgt_name_len
= 0;
224 if (target
->tgt_chap_name
!= NULL
) {
225 kmem_free(target
->tgt_chap_name
,
226 target
->tgt_chap_name_len
);
227 target
->tgt_chap_name
= NULL
;
228 target
->tgt_chap_name_len
= 0;
230 if (target
->tgt_chap_sec
!= NULL
) {
231 kmem_free(target
->tgt_chap_sec
,
232 target
->tgt_chap_sec_len
);
233 target
->tgt_chap_sec
= NULL
;
234 target
->tgt_chap_sec_len
= 0;
236 if (target
->tgt_boot_par
!= NULL
) {
237 kmem_free(target
->tgt_boot_par
,
238 target
->tgt_boot_par_len
);
239 target
->tgt_boot_par
= NULL
;
240 target
->tgt_boot_par_len
= 0;
245 * Free the memory used by boot property.
248 iscsi_boot_prop_free()
252 if (iscsiboot_prop
== NULL
) {
255 tmp
= iscsiboot_prop
;
256 iscsiboot_prop
= NULL
;
257 iscsi_boot_free_ini(&(tmp
->boot_init
));
258 iscsi_boot_free_tgt(&(tmp
->boot_tgt
));
262 kinet_ntoa(char *buf
, void *in
, int af
)
264 unsigned char *p
= NULL
;
267 if (buf
== NULL
|| in
== NULL
) {
270 p
= (unsigned char *)in
;
272 (void) sprintf(buf
, "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
274 for (i
= 0; i
< 14; i
= i
+ 2) {
275 (void) sprintf(buf
, "%02x%02x:", p
[i
], p
[i
+1]);
278 (void) sprintf(buf
, "%02x%02x", p
[i
], p
[i
+1]);
282 #ifndef BO_MAXOBJNAME
283 #define BO_MAXOBJNAME 256
286 #ifndef ISCSI_BOOT_ISID
287 #define ISCSI_BOOT_ISID "0000"
291 * Generate the 'ssd' bootpath of an iSCSI boot device
292 * The caller is responsible to alloc the buf with BO_MAXOBJNAME length
295 get_iscsi_bootpath_vhci(char *bootpath
)
299 if (iscsiboot_prop
== NULL
)
301 if (iscsiboot_prop
== NULL
)
303 lun_num
= (uint16_t *)(&iscsiboot_prop
->boot_tgt
.tgt_boot_lun
[0]);
304 (void) snprintf(bootpath
, BO_MAXOBJNAME
, "/iscsi/ssd@%s%s%04X,%d:%s",
305 ISCSI_BOOT_ISID
, iscsiboot_prop
->boot_tgt
.tgt_name
,
306 iscsiboot_prop
->boot_tgt
.tgt_tpgt
, lun_num
[0],
307 iscsiboot_prop
->boot_tgt
.tgt_boot_par
);
311 * Generate the 'disk' bootpath of an iSCSI boot device
312 * The caller is responsible to alloc the buf with BO_MAXOBJNAME length
315 get_iscsi_bootpath_phy(char *bootpath
)
317 uint16_t lun_num
= 0;
318 uchar_t replaced_name
[BO_MAXOBJNAME
] = {0};
320 if (iscsiboot_prop
== NULL
)
322 if (iscsiboot_prop
== NULL
)
324 if (replace_sp_c(replaced_name
, iscsiboot_prop
->boot_tgt
.tgt_name
,
325 iscsiboot_prop
->boot_tgt
.tgt_name_len
) != 0) {
328 lun_num
= *(uint16_t *)(&iscsiboot_prop
->boot_tgt
.tgt_boot_lun
[0]);
329 (void) snprintf(bootpath
, BO_MAXOBJNAME
, "/iscsi/disk@%s%s%04X,%d:%s",
330 ISCSI_BOOT_ISID
, replaced_name
, iscsiboot_prop
->boot_tgt
.tgt_tpgt
,
331 lun_num
, iscsiboot_prop
->boot_tgt
.tgt_boot_par
);
334 static int replace_sp_c(unsigned char *dst
, unsigned char *source
, size_t n
)
336 unsigned char *p
= NULL
;
339 if (source
== NULL
|| dst
== NULL
|| n
== 0) {
343 for (p
= source
; *p
!= '\0'; p
++, i
++) {