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>
32 #include <sys/bootprops.h>
33 #include <sys/cmn_err.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
38 extern void *memset(void *s
, int c
, size_t n
);
39 extern int memcmp(const void *s1
, const void *s2
, size_t n
);
40 extern void bcopy(const void *s1
, void *s2
, size_t n
);
41 extern size_t strlen(const char *s
);
42 static void kinet_ntoa(char *buf
, void *in
, int af
);
43 extern ib_boot_prop_t
*iscsiboot_prop
;
45 int iscsi_print_bootprop
= 0;
47 #define ISCSI_BOOTPROP_BUFLEN 256
53 static int replace_sp_c(unsigned char *dst
, unsigned char *source
, size_t n
);
56 iscsi_bootprop_print(int level
, char *str
)
61 if (iscsi_print_bootprop
== 1) {
62 cmn_err(level
, "%s", str
);
67 iscsi_print_initiator_property(ib_ini_prop_t
*ibinitp
)
69 char outbuf
[ISCSI_BOOTPROP_BUFLEN
] = {0};
71 if (ibinitp
== NULL
) {
75 if (ibinitp
->ini_name
!= NULL
) {
76 (void) sprintf(outbuf
,
77 "Initiator Name : %s\n",
79 iscsi_bootprop_print(CE_CONT
, outbuf
);
82 if (ibinitp
->ini_chap_name
!= NULL
) {
83 (void) memset(outbuf
, 0, ISCSI_BOOTPROP_BUFLEN
);
84 (void) sprintf(outbuf
,
85 "Initiator CHAP Name : %s\n",
86 ibinitp
->ini_chap_name
);
88 iscsi_bootprop_print(CE_CONT
, outbuf
);
93 iscsi_print_nic_property(ib_nic_prop_t
*nicp
)
95 char outbuf
[ISCSI_BOOTPROP_BUFLEN
] = {0};
96 char ipaddr
[50] = {0};
103 kinet_ntoa(ipaddr
, &nicp
->nic_ip_u
, nicp
->sin_family
);
104 n
= snprintf(outbuf
, ISCSI_BOOTPROP_BUFLEN
,
105 "Local IP addr : %s\n", ipaddr
);
107 (void) memset(ipaddr
, 0, 50);
108 kinet_ntoa(ipaddr
, &nicp
->nic_gw_u
, nicp
->sin_family
);
109 n
= n
+ snprintf(outbuf
+ n
, ISCSI_BOOTPROP_BUFLEN
- n
,
110 "Local gateway : %s\n", ipaddr
);
112 (void) memset(ipaddr
, 0, 50);
113 kinet_ntoa(ipaddr
, &nicp
->nic_dhcp_u
, nicp
->sin_family
);
114 n
= n
+ snprintf(outbuf
+ n
, ISCSI_BOOTPROP_BUFLEN
- n
,
115 "Local DHCP : %s\n", ipaddr
);
117 (void) snprintf(outbuf
+ n
, ISCSI_BOOTPROP_BUFLEN
- n
,
118 "Local MAC : %02x:%02x:%02x:%02x:%02x:%02x\n",
126 iscsi_bootprop_print(CE_CONT
, outbuf
);
130 iscsi_print_tgt_property(ib_tgt_prop_t
*itgtp
)
132 char outbuf
[ISCSI_BOOTPROP_BUFLEN
] = {0};
133 char ipaddr
[50] = {0};
139 if (itgtp
->tgt_name
!= NULL
) {
140 (void) memset(outbuf
, 0, ISCSI_BOOTPROP_BUFLEN
);
141 (void) sprintf(outbuf
,
142 "Target Name : %s\n",
144 iscsi_bootprop_print(CE_CONT
, outbuf
);
147 kinet_ntoa(ipaddr
, &itgtp
->tgt_ip_u
, itgtp
->sin_family
);
148 (void) sprintf(outbuf
,
151 "Boot LUN : %02x%02x-%02x%02x-%02x%02x-%02x%02x\n",
154 itgtp
->tgt_boot_lun
[0],
155 itgtp
->tgt_boot_lun
[1],
156 itgtp
->tgt_boot_lun
[2],
157 itgtp
->tgt_boot_lun
[3],
158 itgtp
->tgt_boot_lun
[4],
159 itgtp
->tgt_boot_lun
[5],
160 itgtp
->tgt_boot_lun
[6],
161 itgtp
->tgt_boot_lun
[7]);
162 iscsi_bootprop_print(CE_CONT
, outbuf
);
164 if (itgtp
->tgt_chap_name
!= NULL
) {
165 (void) memset(outbuf
, 0, ISCSI_BOOTPROP_BUFLEN
);
166 (void) sprintf(outbuf
,
168 itgtp
->tgt_chap_name
);
169 iscsi_bootprop_print(CE_CONT
, outbuf
);
174 iscsi_print_boot_property()
176 if (iscsiboot_prop
== NULL
) {
180 iscsi_print_initiator_property(
181 &iscsiboot_prop
->boot_init
);
183 iscsi_print_nic_property(&iscsiboot_prop
->boot_nic
);
185 iscsi_print_tgt_property(&iscsiboot_prop
->boot_tgt
);
189 iscsi_boot_free_ini(ib_ini_prop_t
*init
)
195 if (init
->ini_name
!= NULL
) {
196 kmem_free(init
->ini_name
, init
->ini_name_len
);
197 init
->ini_name
= NULL
;
198 init
->ini_name_len
= 0;
200 if (init
->ini_chap_name
!= NULL
) {
201 kmem_free(init
->ini_chap_name
,
202 init
->ini_chap_name_len
);
203 init
->ini_chap_name
= NULL
;
204 init
->ini_chap_name_len
= 0;
206 if (init
->ini_chap_sec
!= NULL
) {
207 kmem_free(init
->ini_chap_sec
,
208 init
->ini_chap_sec_len
);
209 init
->ini_chap_sec
= NULL
;
210 init
->ini_chap_sec_len
= 0;
215 iscsi_boot_free_tgt(ib_tgt_prop_t
*target
)
217 if (target
== NULL
) {
221 if (target
->tgt_name
!= NULL
) {
222 kmem_free(target
->tgt_name
,
223 target
->tgt_name_len
);
224 target
->tgt_name
= NULL
;
225 target
->tgt_name_len
= 0;
227 if (target
->tgt_chap_name
!= NULL
) {
228 kmem_free(target
->tgt_chap_name
,
229 target
->tgt_chap_name_len
);
230 target
->tgt_chap_name
= NULL
;
231 target
->tgt_chap_name_len
= 0;
233 if (target
->tgt_chap_sec
!= NULL
) {
234 kmem_free(target
->tgt_chap_sec
,
235 target
->tgt_chap_sec_len
);
236 target
->tgt_chap_sec
= NULL
;
237 target
->tgt_chap_sec_len
= 0;
239 if (target
->tgt_boot_par
!= NULL
) {
240 kmem_free(target
->tgt_boot_par
,
241 target
->tgt_boot_par_len
);
242 target
->tgt_boot_par
= NULL
;
243 target
->tgt_boot_par_len
= 0;
248 * Free the memory used by boot property.
251 iscsi_boot_prop_free()
255 if (iscsiboot_prop
== NULL
) {
258 tmp
= iscsiboot_prop
;
259 iscsiboot_prop
= NULL
;
260 iscsi_boot_free_ini(&(tmp
->boot_init
));
261 iscsi_boot_free_tgt(&(tmp
->boot_tgt
));
265 kinet_ntoa(char *buf
, void *in
, int af
)
267 unsigned char *p
= NULL
;
270 if (buf
== NULL
|| in
== NULL
) {
273 p
= (unsigned char *)in
;
275 (void) sprintf(buf
, "%d.%d.%d.%d", p
[0], p
[1], p
[2], p
[3]);
277 for (i
= 0; i
< 14; i
= i
+ 2) {
278 (void) sprintf(buf
, "%02x%02x:", p
[i
], p
[i
+1]);
281 (void) sprintf(buf
, "%02x%02x", p
[i
], p
[i
+1]);
285 #ifndef BO_MAXOBJNAME
286 #define BO_MAXOBJNAME 256
289 #ifndef ISCSI_BOOT_ISID
290 #define ISCSI_BOOT_ISID "0000"
294 * Generate the 'ssd' bootpath of an iSCSI boot device
295 * The caller is responsible to alloc the buf with BO_MAXOBJNAME length
298 get_iscsi_bootpath_vhci(char *bootpath
)
302 if (iscsiboot_prop
== NULL
)
304 if (iscsiboot_prop
== NULL
)
306 lun_num
= (uint16_t *)(&iscsiboot_prop
->boot_tgt
.tgt_boot_lun
[0]);
307 (void) snprintf(bootpath
, BO_MAXOBJNAME
, "/iscsi/ssd@%s%s%04X,%d:%s",
308 ISCSI_BOOT_ISID
, iscsiboot_prop
->boot_tgt
.tgt_name
,
309 iscsiboot_prop
->boot_tgt
.tgt_tpgt
, lun_num
[0],
310 iscsiboot_prop
->boot_tgt
.tgt_boot_par
);
314 * Generate the 'disk' bootpath of an iSCSI boot device
315 * The caller is responsible to alloc the buf with BO_MAXOBJNAME length
318 get_iscsi_bootpath_phy(char *bootpath
)
320 uint16_t lun_num
= 0;
321 uchar_t replaced_name
[BO_MAXOBJNAME
] = {0};
323 if (iscsiboot_prop
== NULL
)
325 if (iscsiboot_prop
== NULL
)
327 if (replace_sp_c(replaced_name
, iscsiboot_prop
->boot_tgt
.tgt_name
,
328 iscsiboot_prop
->boot_tgt
.tgt_name_len
) != 0) {
331 lun_num
= *(uint16_t *)(&iscsiboot_prop
->boot_tgt
.tgt_boot_lun
[0]);
332 (void) snprintf(bootpath
, BO_MAXOBJNAME
, "/iscsi/disk@%s%s%04X,%d:%s",
333 ISCSI_BOOT_ISID
, replaced_name
, iscsiboot_prop
->boot_tgt
.tgt_tpgt
,
334 lun_num
, iscsiboot_prop
->boot_tgt
.tgt_boot_par
);
337 static int replace_sp_c(unsigned char *dst
, unsigned char *source
, size_t n
)
339 unsigned char *p
= NULL
;
342 if (source
== NULL
|| dst
== NULL
|| n
== 0) {
346 for (p
= source
; *p
!= '\0'; p
++, i
++) {