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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 * Command line options to rmformat are processed in this file.
32 #include <sys/smedia.h>
33 #include <priv_utils.h>
35 extern int32_t D_flag
;
36 extern int32_t e_flag
;
37 extern int32_t H_flag
;
38 extern int32_t U_flag
;
39 extern int32_t V_flag
;
40 extern int32_t b_flag
;
41 extern int32_t w_flag
;
42 extern int32_t W_flag
;
43 extern int32_t s_flag
;
44 extern int32_t c_flag
;
45 extern int32_t F_flag
;
46 extern int32_t R_flag
;
47 extern int32_t p_flag
;
48 extern int32_t l_flag
;
51 extern char *slice_file
;
52 extern diskaddr_t repair_blk_no
;
53 extern int32_t quick_format
;
54 extern int32_t long_format
;
55 extern int32_t force_format
;
56 extern int32_t rw_protect_enable
;
57 extern int32_t rw_protect_disable
;
58 extern int32_t wp_enable_passwd
;
59 extern int32_t wp_disable_passwd
;
60 extern int32_t wp_enable
;
61 extern int32_t wp_disable
;
62 extern int32_t verify_write
;
63 extern char *dev_name
;
65 extern int total_devices_found
;
66 extern int removable_found
;
67 char *global_intr_msg
;
68 smmedium_prop_t med_info
;
71 extern void check_invalid_combinations();
72 extern void check_invalid_combinations_again(int32_t);
73 extern void process_options();
74 extern void get_passwd(struct smwp_state
*wp
, int32_t confirm
);
75 extern int32_t valid_slice_file(smedia_handle_t
, int32_t, char *,
77 extern void trap_SIGINT();
78 extern void release_SIGINT();
79 extern int32_t verify(smedia_handle_t handle
, int32_t fd
,
80 diskaddr_t start_sector
, uint32_t nblocks
,
81 char *buf
, int32_t flag
, int32_t blocksize
, int32_t no_raw_rw
);
82 extern void my_perror(char *err_string
);
83 extern void write_default_label(smedia_handle_t
, int32_t fd
);
84 extern int find_device(int defer
, char *tmpstr
);
86 void overwrite_metadata(int32_t fd
, smedia_handle_t handle
);
88 int32_t write_sunos_label(int32_t fd
, int32_t media_type
);
90 int32_t my_open(char *device_name
, int32_t flags
);
91 int32_t check_and_unmount_vold(char *device_name
, int32_t flag
);
92 int32_t check_and_unmount_scsi(char *device_name
, int32_t flag
);
94 int32_t check_and_unmount_floppy(int32_t fd
, int32_t flag
);
95 int32_t get_confirmation(void);
98 static void process_F_flag(smedia_handle_t handle
, int32_t fd
);
99 static void process_w_flag(smedia_handle_t handle
);
100 static void process_W_flag(smedia_handle_t handle
);
101 static void process_R_flag(smedia_handle_t handle
);
102 void process_p_flag(smedia_handle_t handle
, int32_t fd
);
103 static void process_c_flag(smedia_handle_t handle
);
104 static void process_V_flag(smedia_handle_t handle
, int32_t fd
);
105 static void process_s_flag(smedia_handle_t
, int32_t fd
);
106 static void process_e_flag(smedia_handle_t handle
);
107 static void process_H_flag(smedia_handle_t handle
, int32_t fd
);
108 static void process_D_flag(smedia_handle_t handle
, int32_t fd
);
109 static void process_b_flag(int32_t fd
);
110 static void process_l_flag(void);
116 smedia_handle_t handle
;
117 int32_t m_scsi_umount
= 0;
118 int32_t m_flp_umount
= 0;
119 int32_t v_device_umount
= 0;
120 int32_t umount_required
= 0;
122 int32_t umount_failed
= 0;
123 struct dk_minfo media
;
125 check_invalid_combinations();
127 if (l_flag
&& !dev_name
) {
133 if (!(F_flag
|| H_flag
|| D_flag
)) {
139 if (F_flag
|| w_flag
|| W_flag
|| R_flag
|| D_flag
|| H_flag
||
140 V_flag
|| c_flag
|| b_flag
|| s_flag
|| e_flag
) {
144 fd
= my_open(dev_name
, O_RDONLY
|O_NDELAY
);
146 PERROR("Could not open device");
151 if (ioctl(fd
, DKIOCREMOVABLE
, &removable
) < 0) {
152 PERROR("DKIOCREMOVABLE ioctl failed");
157 (void) fprintf(stderr
,
158 gettext("Not a removable media device\n"));
163 if (ioctl(fd
, DKIOCGMEDIAINFO
, &media
) < 0) {
164 (void) fprintf(stderr
,
165 gettext("No media in specified device\n"));
170 /* Check if volume manager has mounted this */
171 if (umount_required
) {
172 v_device_umount
= check_and_unmount_vold(dev_name
, U_flag
);
173 if (v_device_umount
!= 1) {
174 m_scsi_umount
= check_and_unmount_scsi(dev_name
,
176 if (m_scsi_umount
!= 1) {
177 m_flp_umount
= check_and_unmount_floppy(fd
,
179 if (m_flp_umount
!= 1) {
186 if (umount_required
&& U_flag
&& umount_failed
) {
187 if (v_device_umount
|| m_scsi_umount
|| m_flp_umount
) {
188 (void) fprintf(stderr
,
189 gettext("Could not unmount device.\n"));
195 if (umount_required
&& !U_flag
) {
196 if (v_device_umount
|| m_scsi_umount
|| m_flp_umount
) {
197 (void) fprintf(stderr
, gettext("Device mounted.\n"));
198 (void) fprintf(stderr
,
199 gettext("Requested operation can not be \
200 performed on a mounted device.\n"));
205 /* register the fd with the libsmedia */
206 handle
= smedia_get_handle(fd
);
207 if (handle
== NULL
) {
208 (void) fprintf(stderr
,
209 gettext("Failed to get libsmedia handle.\n"));
214 if (smedia_get_medium_property(handle
, &med_info
) < 0) {
215 (void) fprintf(stderr
,
216 gettext("Get medium property failed \n"));
217 (void) smedia_release_handle(handle
);
222 DPRINTF1("media type %x\n", med_info
.sm_media_type
);
223 DPRINTF1("media block size %x\n", med_info
.sm_blocksize
);
224 DPRINTF1("media capacity %u\n", (uint32_t)med_info
.sm_capacity
);
225 DPRINTF3("media cyl %d head %d sect %d\n",
226 med_info
.sm_pcyl
, med_info
.sm_nhead
, med_info
.sm_nsect
);
227 check_invalid_combinations_again(med_info
.sm_media_type
);
230 * Special handling for pcmcia, sometimes open the file in
234 if (med_info
.sm_media_type
== SM_PCMCIA_MEM
) {
235 if (F_flag
|| H_flag
|| D_flag
|| (V_flag
&& verify_write
)) {
237 DPRINTF("Reopening device\n");
238 fd
= my_open(dev_name
, O_RDWR
|O_NDELAY
);
240 PERROR("Could not open device");
241 (void) smedia_release_handle(handle
);
248 if (med_info
.sm_media_type
== SM_PCMCIA_ATA
) {
249 if (V_flag
|| c_flag
) {
250 (void) fprintf(stderr
,
251 gettext("Option not supported on PC ATA cards\n"));
252 (void) smedia_release_handle(handle
);
257 /* same text as used by the format command */
258 (void) fprintf(stderr
,
259 gettext("Cannot format this drive. Please use your \
260 Manufacturer supplied formatting utility.\n"));
261 (void) smedia_release_handle(handle
);
268 process_F_flag(handle
, fd
);
270 process_w_flag(handle
);
272 process_W_flag(handle
);
274 process_R_flag(handle
);
276 process_p_flag(handle
, fd
);
278 process_D_flag(handle
, fd
);
280 process_H_flag(handle
, fd
);
282 process_V_flag(handle
, fd
);
284 process_c_flag(handle
);
288 process_s_flag(handle
, fd
);
290 process_e_flag(handle
);
295 (void) smedia_release_handle(handle
);
300 * This routine handles the F_flag.
301 * This options should not be used for floppy. However,
302 * if this option is used for floppy, the option will
303 * be forced to SM_FORMAT_HD and smedia_format is called.
304 * Note that smedia_format is a blocked mode format and it
305 * returns only after the complete formatting is over.
309 process_F_flag(smedia_handle_t handle
, int32_t fd
)
311 uint32_t format_flag
;
313 int32_t new_per
, ret_val
;
316 (void) fprintf(stderr
,
317 gettext("Formatting disk.\n"));
319 (void) fprintf(stderr
,
320 gettext("Formatting will erase all the data on disk.\n"));
321 if (!get_confirmation())
326 format_flag
= SM_FORMAT_QUICK
;
327 else if (long_format
)
328 format_flag
= SM_FORMAT_LONG
;
329 else if (force_format
)
330 format_flag
= SM_FORMAT_FORCE
;
332 if (med_info
.sm_media_type
== SM_FLOPPY
)
333 format_flag
= SM_FORMAT_HD
;
335 if ((med_info
.sm_media_type
!= SM_FLOPPY
) &&
336 (med_info
.sm_media_type
!= SM_PCMCIA_MEM
) &&
337 (med_info
.sm_media_type
!= SM_SCSI_FLOPPY
)) {
338 global_intr_msg
= "Interrupting format may render the \
341 global_intr_msg
= "";
345 if (smedia_format(handle
, format_flag
, SM_FORMAT_IMMEDIATE
) != 0) {
346 if (errno
== EINVAL
) {
347 (void) fprintf(stderr
, gettext("Format failed.\n"));
348 (void) fprintf(stderr
, gettext("The medium may not \
349 be compatible for format operation.\n"));
350 (void) fprintf(stderr
, gettext("read/write surface \
351 scan may be used to get the effect of formatting.\n"));
353 PERROR("Format failed");
355 (void) smedia_release_handle(handle
);
362 ret_val
= smedia_check_format_status(handle
);
364 if (errno
!= ENOTSUP
) {
365 PERROR("Format failed");
366 (void) smedia_release_handle(handle
);
370 /* Background formatting is not supported */
374 if (ret_val
== 100) {
376 (void) fflush(stdout
);
379 new_per
= (ret_val
* 80)/100;
380 while (new_per
>= old_per
) {
382 (void) fflush(stdout
);
388 if ((med_info
.sm_media_type
== SM_FLOPPY
) ||
389 (med_info
.sm_media_type
== SM_PCMCIA_MEM
) ||
390 (med_info
.sm_media_type
== SM_SCSI_FLOPPY
)) {
391 (void) write_sunos_label(fd
, med_info
.sm_media_type
);
395 * Iomega drives don't destroy the data in quick format.
396 * Do a best effort write to first 1024 sectors.
400 overwrite_metadata(fd
, handle
);
402 (void) write_default_label(handle
, fd
);
409 * List removable devices.
416 int total_devices_found_last_time
;
420 #define MAX_RETRIES_FOR_SCANNING 3
422 vol_running
= volmgt_running();
425 (void) printf(gettext("Looking for devices...\n"));
426 total_devices_found_last_time
= 0;
429 * Strip out any leading path. For example, /dev/rdsk/c3t0d0s2
430 * will result in tmpstr = c3t0d0s2. dev_name is given as input
434 if ((tmpstr
= strrchr(dev_name
, '/')) != NULL
) {
435 tmpstr
+= sizeof (char);
441 for (retry
= 0; retry
< MAX_RETRIES_FOR_SCANNING
; retry
++) {
442 removable
= find_device(defer
, tmpstr
);
447 * We'll do a small sleep and retry the command if volume
448 * manager is running and no removable devices are found.
449 * This is because the device may be busy.
451 if (defer
|| (vol_running
&& (removable
== 0))) {
452 if ((total_devices_found
== 0) ||
453 (total_devices_found
!=
454 total_devices_found_last_time
)) {
455 total_devices_found_last_time
=
459 /* Do the printing this time */
467 if (removable_found
== 0)
468 (void) printf(gettext("No removables found.\n"));
472 * The following three routines handle the write protect
473 * options. These options are mostly Iomega ZIP/Jaz centric.
474 * The following options are allowed :
475 * No write protect <=> write protect without passwd : use -w flag
476 * from any state to WP with passwd : use -W flag
477 * from WP with passwd to no write protect : use -W flag
478 * from any state to RWP with passwd : use -R flag
479 * from RWP with passwd to no write protect : use -R flag
481 * The following transitions is not allowed
482 * WP with passwd or RWP to WP without passwd.
486 process_w_flag(smedia_handle_t handle
)
490 struct smwp_state wps
;
492 if ((rval
= smedia_get_protection_status((handle
), &wps
)) < 0) {
493 (void) fprintf(stderr
,
494 gettext("Could not get medium status \n"));
497 med_status
= wps
.sm_new_state
;
499 wps
.sm_version
= SMWP_STATE_V_1
;
501 if (wp_enable
) { /* Enable write protect no password */
503 switch (med_status
) {
504 case SM_WRITE_PROTECT_DISABLE
:
506 SM_WRITE_PROTECT_NOPASSWD
;
507 wps
.sm_passwd_len
= 0;
508 rval
= smedia_set_protection_status(handle
,
513 case SM_WRITE_PROTECT_NOPASSWD
:
514 (void) fprintf(stderr
, gettext(WP_MSG_0
));
516 case SM_WRITE_PROTECT_PASSWD
:
517 (void) fprintf(stderr
, gettext(WP_MSG_1
));
519 case SM_READ_WRITE_PROTECT
:
520 (void) fprintf(stderr
, gettext(WP_MSG_2
));
522 case SM_STATUS_UNKNOWN
:
524 (void) fprintf(stderr
, gettext(WP_UNKNOWN
));
527 } else if (wp_disable
) {
528 switch (med_status
) {
529 case SM_WRITE_PROTECT_NOPASSWD
:
531 SM_WRITE_PROTECT_DISABLE
;
532 wps
.sm_passwd_len
= 0;
533 rval
= smedia_set_protection_status(handle
,
538 case SM_WRITE_PROTECT_DISABLE
:
539 (void) fprintf(stderr
, gettext(WP_MSG_3
));
541 case SM_WRITE_PROTECT_PASSWD
:
542 (void) fprintf(stderr
, gettext(WP_MSG_1
));
544 case SM_READ_WRITE_PROTECT
:
545 (void) fprintf(stderr
, gettext(WP_MSG_2
));
547 case SM_STATUS_UNKNOWN
:
549 (void) fprintf(stderr
, gettext(WP_UNKNOWN
));
556 process_W_flag(smedia_handle_t handle
)
560 struct smwp_state wps
;
562 DPRINTF("Write protect with password\n");
564 if ((rval
= smedia_get_protection_status((handle
), &wps
)) < 0) {
565 (void) fprintf(stderr
,
566 gettext("Could not get medium status \n"));
569 med_status
= wps
.sm_new_state
;
571 wps
.sm_version
= SMWP_STATE_V_1
;
573 if (wp_enable_passwd
) { /* Enable write protect */
574 switch (med_status
) {
575 case SM_WRITE_PROTECT_DISABLE
:
576 case SM_WRITE_PROTECT_NOPASSWD
:
577 DPRINTF("Getting passwd\n");
580 SM_WRITE_PROTECT_PASSWD
;
581 rval
= smedia_set_protection_status(handle
,
587 case SM_READ_WRITE_PROTECT
:
588 (void) fprintf(stderr
, gettext(WP_MSG_4
));
589 (void) fprintf(stderr
, gettext(WP_MSG_5
));
592 SM_WRITE_PROTECT_PASSWD
;
593 rval
= smedia_set_protection_status(handle
,
596 if (errno
== EACCES
) {
597 (void) fprintf(stderr
,
604 case SM_WRITE_PROTECT_PASSWD
:
605 (void) fprintf(stderr
, gettext(WP_MSG_6
));
607 case SM_STATUS_UNKNOWN
:
609 (void) fprintf(stderr
,
610 gettext(WP_UNKNOWN
));
613 } else if (wp_disable_passwd
) {
614 switch (med_status
) {
615 case SM_WRITE_PROTECT_PASSWD
:
618 SM_WRITE_PROTECT_DISABLE
;
619 rval
= smedia_set_protection_status(handle
,
622 if (errno
== EACCES
) {
623 (void) fprintf(stderr
,
630 case SM_READ_WRITE_PROTECT
:
631 (void) fprintf(stderr
, gettext(WP_MSG_2
));
633 case SM_WRITE_PROTECT_NOPASSWD
:
634 (void) fprintf(stderr
, gettext(WP_MSG_7
));
636 case SM_WRITE_PROTECT_DISABLE
:
637 (void) fprintf(stderr
, gettext(WP_MSG_3
));
639 case SM_STATUS_UNKNOWN
:
641 (void) fprintf(stderr
, gettext(WP_UNKNOWN
));
648 process_R_flag(smedia_handle_t handle
)
652 struct smwp_state wps
;
654 DPRINTF("Read Write protect \n");
656 if ((rval
= smedia_get_protection_status((handle
), &wps
)) < 0) {
657 (void) fprintf(stderr
,
658 gettext("Could not get medium status \n"));
661 med_status
= wps
.sm_new_state
;
663 wps
.sm_version
= SMWP_STATE_V_1
;
665 if (rw_protect_enable
) { /* Enable write protect */
666 switch (med_status
) {
667 case SM_WRITE_PROTECT_DISABLE
:
668 case SM_WRITE_PROTECT_NOPASSWD
:
669 DPRINTF("Getting passwd\n");
672 SM_READ_WRITE_PROTECT
;
673 rval
= smedia_set_protection_status(handle
,
678 case SM_WRITE_PROTECT_PASSWD
:
679 (void) fprintf(stderr
, gettext(WP_MSG_8
));
680 (void) fprintf(stderr
, gettext(WP_MSG_9
));
683 SM_READ_WRITE_PROTECT
;
684 rval
= smedia_set_protection_status(handle
,
687 if (errno
== EACCES
) {
688 (void) fprintf(stderr
,
695 case SM_READ_WRITE_PROTECT
:
696 (void) fprintf(stderr
, gettext(WP_MSG_4
));
698 case SM_STATUS_UNKNOWN
:
700 (void) fprintf(stderr
, gettext(WP_UNKNOWN
));
703 } else if (rw_protect_disable
) {
704 switch (med_status
) {
705 case SM_READ_WRITE_PROTECT
:
706 case SM_STATUS_UNKNOWN
:
709 SM_WRITE_PROTECT_DISABLE
;
710 rval
= smedia_set_protection_status(handle
,
713 if (errno
== EACCES
) {
714 (void) fprintf(stderr
,
721 case SM_WRITE_PROTECT_PASSWD
:
722 (void) fprintf(stderr
, gettext(WP_MSG_1
));
724 case SM_WRITE_PROTECT_NOPASSWD
:
725 (void) fprintf(stderr
, gettext(WP_MSG_7
));
727 case SM_WRITE_PROTECT_DISABLE
:
728 (void) fprintf(stderr
, gettext(WP_MSG_3
));
731 (void) fprintf(stderr
, gettext(WP_UNKNOWN
));
738 process_p_flag(smedia_handle_t handle
, int32_t fd
)
743 med_status
= smedia_get_protection_status((handle
), &wps
);
744 DPRINTF("Could not get medium status \n");
747 * Workaround in case mode sense fails.
749 * Also, special handling for PCMCIA. PCMCIA does not have any
750 * ioctl to find out the write protect status. So, open the
751 * device with O_RDWR. If it passes, it is not write protected,
752 * otherwise it is write protected.
753 * If it fails, reopen with O_RDONLY, may be some other
754 * operation can go through.
756 if ((med_status
< 0) || (med_info
.sm_media_type
== SM_PCMCIA_MEM
) ||
757 (med_info
.sm_media_type
== SM_PCMCIA_ATA
)) {
759 DPRINTF("Reopening device for -p option\n");
760 fd
= my_open(dev_name
, O_RDONLY
|O_NDELAY
);
763 PERROR("Could not open device");
764 (void) smedia_release_handle(handle
);
768 (void) fprintf(stdout
,
769 gettext("<Unknown>\n"));
770 (void) smedia_release_handle(handle
);
774 fd
= my_open(dev_name
, O_RDWR
|O_NDELAY
);
776 (void) fprintf(stdout
,
777 gettext("Medium is write protected.\n"));
779 } else { /* Open succeeded */
780 (void) fprintf(stdout
,
781 gettext("Medium is not write protected.\n"));
785 med_status
= wps
.sm_new_state
;
786 switch (med_status
) {
788 case SM_READ_WRITE_PROTECT
:
789 (void) fprintf(stdout
,
790 gettext("Medium is read-write protected.\n"));
792 case SM_WRITE_PROTECT_PASSWD
:
793 (void) fprintf(stdout
,
794 gettext("Medium is write protected with password.\n"));
796 case SM_WRITE_PROTECT_NOPASSWD
:
797 (void) fprintf(stdout
,
798 gettext("Medium is write protected.\n"));
800 case SM_WRITE_PROTECT_DISABLE
:
801 (void) fprintf(stdout
,
802 gettext("Medium is not write protected.\n"));
804 case SM_STATUS_UNKNOWN
:
806 (void) fprintf(stdout
,
807 gettext("Unknown write protect status.\n"));
813 process_c_flag(smedia_handle_t handle
)
815 char error_string
[256];
817 if (smedia_reassign_block(handle
, repair_blk_no
) != 0) {
818 (void) snprintf(error_string
, 255,
819 gettext("Could not repair block no %llu"), repair_blk_no
);
820 PERROR(error_string
);
826 * This routine handles the -V (verify) option.
827 * There can be devices without rw_read option. If the raw_read
828 * and raw_write are not supported by the interface, then read and
829 * write system calls are used. It is assumed that either both
830 * raw_read and raw_write are supported or both are unsupported.
834 process_V_flag(smedia_handle_t handle
, int32_t fd
)
839 char *read_buf
, *write_buf
;
842 int32_t no_raw_rw
= 0;
847 DPRINTF("ANALYSE MEDIA \n");
849 ret
= smedia_get_medium_property(handle
, &med_info
);
851 DPRINTF("get_media_info failed\n");
855 DPRINTF1("media_type %d\n", med_info
.sm_media_type
);
856 DPRINTF1("sector_size %d\n", med_info
.sm_blocksize
);
857 DPRINTF1("num_sectors %u\n", (uint32_t)med_info
.sm_capacity
);
858 DPRINTF1("nsect %d\n", med_info
.sm_nsect
);
860 blocksize
= med_info
.sm_blocksize
;
862 capacity
= (uint32_t)med_info
.sm_capacity
;
863 verify_size
= (med_info
.sm_nsect
> 64) ? 64 : med_info
.sm_nsect
;
864 read_buf
= (char *)malloc(blocksize
* verify_size
);
865 if (read_buf
== NULL
) {
866 DPRINTF("Could not allocate memory\n");
869 write_buf
= (char *)malloc(blocksize
* verify_size
);
870 if (write_buf
== NULL
) {
871 DPRINTF("Could not allocate memory\n");
877 DPRINTF("Non-destructive verify \n");
878 for (bn
= 0; bn
< (uint32_t)med_info
.sm_capacity
;
880 new_per
= (bn
* 80)/(uint32_t)med_info
.sm_capacity
;
881 if (new_per
>= old_per
) {
883 (void) fflush(stdout
);
886 DPRINTF2("Reading %d blks starting at %llu\n",
888 ret
= verify(handle
, fd
, bn
, verify_size
, read_buf
,
889 VERIFY_READ
, blocksize
, no_raw_rw
);
890 if ((ret
== -1) && (errno
== ENOTSUP
)) {
892 ret
= verify(handle
, fd
, bn
, verify_size
,
894 VERIFY_READ
, blocksize
, no_raw_rw
);
895 capacity
= (diskaddr_t
)med_info
.sm_pcyl
*
896 med_info
.sm_nhead
* med_info
.sm_nsect
;
900 for (j
= 0; j
< verify_size
; j
++) {
901 if ((bn
+ j
) >= capacity
)
904 "Reading %d blks starting "
905 "at %llu\n", 1, bn
+ j
);
906 ret
= verify(handle
, fd
, bn
+ j
, 1,
908 VERIFY_READ
, blocksize
,
920 DPRINTF("Destrutive verify \n");
921 for (bn
= 0; bn
< (uint32_t)med_info
.sm_capacity
;
923 new_per
= (bn
* 80)/(uint32_t)med_info
.sm_capacity
;
924 if (new_per
>= old_per
) {
927 (void) fflush(stdout
);
931 for (j
= 0; j
< blocksize
* verify_size
; j
++) {
932 write_buf
[j
] = (bn
| j
) & 0xFF;
934 DPRINTF2("Writing %d blks starting at %llu\n",
936 ret
= verify(handle
, fd
, bn
, verify_size
, write_buf
,
937 VERIFY_WRITE
, blocksize
, no_raw_rw
);
940 for (j
= 0; j
< verify_size
; j
++) {
941 if ((bn
+ j
) >= capacity
)
944 "Writing %d blks starting "
945 "at %llu\n", 1, bn
+ j
);
946 ret
= verify(handle
, fd
, bn
+ j
, 1,
948 VERIFY_WRITE
, blocksize
,
952 "Bad block %llu\n", bn
+ j
);
956 DPRINTF2("Read after write %d blks starting at %llu\n",
958 ret
= verify(handle
, fd
, bn
, verify_size
,
959 read_buf
, VERIFY_READ
, blocksize
, no_raw_rw
);
962 for (j
= 0; j
< verify_size
; j
++) {
963 if ((bn
+ j
) >= capacity
)
966 "Read after write %d blks "
967 "starting at %llu\n", 1, bn
+ j
);
968 ret
= verify(handle
, fd
, bn
+ j
, 1,
969 read_buf
, VERIFY_READ
,
970 blocksize
, no_raw_rw
);
973 "Bad block %llu\n", bn
+ j
);
984 process_s_flag(smedia_handle_t handle
, int32_t fd
)
987 struct extvtoc v_toc
, t_vtoc
;
988 if (valid_slice_file(handle
, fd
, slice_file
, &v_toc
)) {
989 (void) smedia_release_handle(handle
);
994 (void) memset(&t_vtoc
, 0, sizeof (t_vtoc
));
997 t_vtoc
.v_nparts
= V_NUMPAR
;
998 t_vtoc
.v_sanity
= VTOC_SANE
;
999 t_vtoc
.v_version
= V_VERSION
;
1000 t_vtoc
.v_sectorsz
= DEV_BSIZE
;
1002 /* Get existing Vtoc, don't bother if it fails. */
1004 /* Turn on privileges. */
1005 (void) __priv_bracket(PRIV_ON
);
1007 (void) read_extvtoc(fd
, &t_vtoc
);
1009 /* Turn off privileges. */
1010 (void) __priv_bracket(PRIV_OFF
);
1012 for (i
= 0; i
< V_NUMPAR
; i
++) {
1013 t_vtoc
.v_part
[i
].p_start
= v_toc
.v_part
[i
].p_start
;
1014 t_vtoc
.v_part
[i
].p_size
= v_toc
.v_part
[i
].p_size
;
1015 t_vtoc
.v_part
[i
].p_tag
= v_toc
.v_part
[i
].p_tag
;
1016 t_vtoc
.v_part
[i
].p_flag
= v_toc
.v_part
[i
].p_flag
;
1022 /* Turn on privileges. */
1023 (void) __priv_bracket(PRIV_ON
);
1025 ret
= write_extvtoc(fd
, &t_vtoc
);
1027 /* Turn off privileges. */
1028 (void) __priv_bracket(PRIV_OFF
);
1032 PERROR("write VTOC failed");
1033 DPRINTF1("Errno = %d\n", errno
);
1036 PERROR("No Solaris partition, eject & retry");
1037 DPRINTF1("Errno = %d\n", errno
);
1039 PERROR("write VTOC failed");
1040 DPRINTF1("Errno = %d\n", errno
);
1046 process_e_flag(smedia_handle_t handle
)
1048 if (smedia_eject(handle
) < 0) {
1049 PERROR("Eject failed");
1053 process_H_flag(smedia_handle_t handle
, int32_t fd
)
1056 int32_t old_per
= 0;
1059 (void) fprintf(stderr
,
1060 gettext("Formatting will erase all the data on disk.\n"));
1061 if (!get_confirmation())
1064 for (cyl
= 0; cyl
< med_info
.sm_pcyl
; cyl
++) {
1065 for (head
= 0; head
< med_info
.sm_nhead
; head
++) {
1066 if (smedia_format_track(handle
, cyl
, head
, SM_FORMAT_HD
)
1068 PERROR("Format failed");
1072 new_per
= (cyl
* 80)/med_info
.sm_pcyl
;
1073 while (new_per
>= old_per
) {
1075 (void) fflush(stdout
);
1080 (void) write_sunos_label(fd
, med_info
.sm_media_type
);
1084 process_D_flag(smedia_handle_t handle
, int32_t fd
)
1087 int32_t old_per
= 0;
1090 (void) fprintf(stderr
,
1091 gettext("Formatting will erase all the data on disk.\n"));
1092 if (!get_confirmation())
1094 for (cyl
= 0; cyl
< med_info
.sm_pcyl
; cyl
++) {
1095 for (head
= 0; head
< med_info
.sm_nhead
; head
++) {
1096 if (smedia_format_track(handle
, cyl
, head
, SM_FORMAT_DD
)
1098 PERROR("Format failed");
1102 new_per
= (cyl
* 80)/med_info
.sm_pcyl
;
1103 while (new_per
>= old_per
) {
1105 (void) fflush(stdout
);
1109 (void) write_sunos_label(fd
, med_info
.sm_media_type
);
1113 * This routine handles the -b (label) option.
1114 * Please note that, this will fail if there is no valid vtoc is
1115 * there on the medium and the vtoc is not faked.
1119 process_b_flag(int32_t fd
)
1121 int32_t ret
, nparts
;
1122 struct extvtoc v_toc
;
1123 struct dk_gpt
*vtoc64
;
1125 /* For EFI disks. */
1127 if (efi_alloc_and_read(fd
, &vtoc64
) < 0) {
1129 * If reading the vtoc failed, try to
1130 * auto-sense the disk configuration.
1132 if (efi_auto_sense(fd
, &vtoc64
) < 0) {
1133 (void) fprintf(stderr
,
1134 gettext("Could not write label.\n"));
1138 for (nparts
= 0; nparts
< vtoc64
->efi_nparts
;
1140 if (vtoc64
->efi_parts
[nparts
].p_tag
==
1142 if (vtoc64
->efi_parts
[nparts
].p_name
) {
1144 vtoc64
->efi_parts
[nparts
].p_name
, label
,
1150 if (efi_write(fd
, vtoc64
) != 0) {
1151 (void) efi_err_check(vtoc64
);
1152 (void) fprintf(stderr
,
1153 gettext("Could not write label.\n"));
1158 /* Get existing Vtoc */
1160 /* Turn on privileges. */
1161 (void) __priv_bracket(PRIV_ON
);
1163 ret
= read_extvtoc(fd
, &v_toc
);
1165 /* Turn off privileges */
1166 (void) __priv_bracket(PRIV_OFF
);
1170 PERROR("read VTOC failed");
1171 DPRINTF1("Errno = %d\n", errno
);
1174 PERROR("No Solaris partition, eject & retry");
1175 DPRINTF1("Errno = %d\n", errno
);
1177 PERROR("read VTOC failed");
1178 DPRINTF1("Errno = %d\n", errno
);
1184 (void) strncpy(v_toc
.v_volume
, label
, LEN_DKL_VVOL
);
1187 /* Turn on the privileges. */
1188 (void) __priv_bracket(PRIV_ON
);
1190 ret
= write_extvtoc(fd
, &v_toc
);
1192 /* Turn off the privileges. */
1193 (void) __priv_bracket(PRIV_OFF
);
1197 PERROR("write VTOC failed");
1198 DPRINTF1("Errno = %d\n", errno
);
1201 PERROR("No Solaris partition, eject & retry");
1202 DPRINTF1("Errno = %d\n", errno
);
1204 PERROR("write VTOC failed");
1205 DPRINTF1("Errno = %d\n", errno
);