Print this page
4833 Remove volrmmount
Reviewed by: Dan McDonald <danmcd@omniti.com>
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/rmformat/rmf_menu.c
+++ new/usr/src/cmd/rmformat/rmf_menu.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 - */
21 -/*
20 + *
21 + *
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 + *
25 + * Copyright 2014 Andrew Stormont.
24 26 */
25 27
26 28 /*
27 29 * rmf_menu.c :
28 30 * Command line options to rmformat are processed in this file.
29 31 */
30 32
31 33 #include "rmformat.h"
32 34 #include <sys/smedia.h>
33 35 #include <priv_utils.h>
34 36
35 37 extern int32_t D_flag;
36 38 extern int32_t e_flag;
37 39 extern int32_t H_flag;
38 40 extern int32_t U_flag;
39 41 extern int32_t V_flag;
40 42 extern int32_t b_flag;
41 43 extern int32_t w_flag;
42 44 extern int32_t W_flag;
43 45 extern int32_t s_flag;
44 46 extern int32_t c_flag;
45 47 extern int32_t F_flag;
46 48 extern int32_t R_flag;
47 49 extern int32_t p_flag;
48 50 extern int32_t l_flag;
49 51
50 52 extern char *myname;
51 53 extern char *slice_file;
52 54 extern diskaddr_t repair_blk_no;
53 55 extern int32_t quick_format;
54 56 extern int32_t long_format;
55 57 extern int32_t force_format;
56 58 extern int32_t rw_protect_enable;
57 59 extern int32_t rw_protect_disable;
58 60 extern int32_t wp_enable_passwd;
59 61 extern int32_t wp_disable_passwd;
60 62 extern int32_t wp_enable;
61 63 extern int32_t wp_disable;
62 64 extern int32_t verify_write;
63 65 extern char *dev_name;
64 66 extern char *label;
65 67 extern int total_devices_found;
66 68 extern int removable_found;
67 69 char *global_intr_msg;
68 70 smmedium_prop_t med_info;
69 71 int vol_running;
70 72
71 73 extern void check_invalid_combinations();
72 74 extern void check_invalid_combinations_again(int32_t);
73 75 extern void process_options();
74 76 extern void get_passwd(struct smwp_state *wp, int32_t confirm);
75 77 extern int32_t valid_slice_file(smedia_handle_t, int32_t, char *,
76 78 struct extvtoc *);
77 79 extern void trap_SIGINT();
78 80 extern void release_SIGINT();
79 81 extern int32_t verify(smedia_handle_t handle, int32_t fd,
80 82 diskaddr_t start_sector, uint32_t nblocks,
81 83 char *buf, int32_t flag, int32_t blocksize, int32_t no_raw_rw);
82 84 extern void my_perror(char *err_string);
83 85 extern void write_default_label(smedia_handle_t, int32_t fd);
84 86 extern int find_device(int defer, char *tmpstr);
85 87
86 88 void overwrite_metadata(int32_t fd, smedia_handle_t handle);
87 89
88 90 int32_t write_sunos_label(int32_t fd, int32_t media_type);
89 91
90 92 int32_t my_open(char *device_name, int32_t flags);
91 93 int32_t check_and_unmount_vold(char *device_name, int32_t flag);
92 94 int32_t check_and_unmount_scsi(char *device_name, int32_t flag);
93 95
94 96 int32_t check_and_unmount_floppy(int32_t fd, int32_t flag);
95 97 int32_t get_confirmation(void);
96 98
97 99
98 100 static void process_F_flag(smedia_handle_t handle, int32_t fd);
99 101 static void process_w_flag(smedia_handle_t handle);
100 102 static void process_W_flag(smedia_handle_t handle);
101 103 static void process_R_flag(smedia_handle_t handle);
102 104 void process_p_flag(smedia_handle_t handle, int32_t fd);
103 105 static void process_c_flag(smedia_handle_t handle);
104 106 static void process_V_flag(smedia_handle_t handle, int32_t fd);
105 107 static void process_s_flag(smedia_handle_t, int32_t fd);
106 108 static void process_e_flag(smedia_handle_t handle);
107 109 static void process_H_flag(smedia_handle_t handle, int32_t fd);
108 110 static void process_D_flag(smedia_handle_t handle, int32_t fd);
109 111 static void process_b_flag(int32_t fd);
110 112 static void process_l_flag(void);
111 113
112 114 void
113 115 process_options()
114 116 {
115 117 int32_t fd;
116 118 smedia_handle_t handle;
117 119 int32_t m_scsi_umount = 0;
118 120 int32_t m_flp_umount = 0;
119 121 int32_t v_device_umount = 0;
120 122 int32_t umount_required = 0;
121 123 int32_t removable;
122 124 int32_t umount_failed = 0;
123 125 struct dk_minfo media;
124 126
125 127 check_invalid_combinations();
126 128
127 129 if (l_flag && !dev_name) {
128 130 process_l_flag();
129 131 return;
130 132 }
131 133
132 134 if (U_flag) {
133 135 if (!(F_flag || H_flag || D_flag)) {
134 136 F_flag = 1;
135 137 long_format = 1;
136 138 }
137 139 }
138 140
139 141 if (F_flag || w_flag || W_flag || R_flag || D_flag || H_flag ||
140 142 V_flag || c_flag || b_flag || s_flag || e_flag) {
141 143 umount_required = 1;
142 144 }
143 145
144 146 fd = my_open(dev_name, O_RDONLY|O_NDELAY);
145 147 if (fd < 0) {
146 148 PERROR("Could not open device");
147 149 (void) close(fd);
148 150 exit(1);
149 151 }
150 152
151 153 if (ioctl(fd, DKIOCREMOVABLE, &removable) < 0) {
152 154 PERROR("DKIOCREMOVABLE ioctl failed");
153 155 (void) close(fd);
154 156 exit(1);
155 157 }
156 158 if (!removable) {
157 159 (void) fprintf(stderr,
158 160 gettext("Not a removable media device\n"));
159 161 (void) close(fd);
160 162 exit(1);
161 163 }
162 164
163 165 if (ioctl(fd, DKIOCGMEDIAINFO, &media) < 0) {
164 166 (void) fprintf(stderr,
165 167 gettext("No media in specified device\n"));
166 168 (void) close(fd);
167 169 exit(1);
168 170 }
169 171
170 172 /* Check if volume manager has mounted this */
171 173 if (umount_required) {
172 174 v_device_umount = check_and_unmount_vold(dev_name, U_flag);
173 175 if (v_device_umount != 1) {
174 176 m_scsi_umount = check_and_unmount_scsi(dev_name,
175 177 U_flag);
176 178 if (m_scsi_umount != 1) {
177 179 m_flp_umount = check_and_unmount_floppy(fd,
178 180 U_flag);
179 181 if (m_flp_umount != 1) {
180 182 umount_failed = 1;
181 183 }
182 184 }
183 185 }
184 186 }
185 187
186 188 if (umount_required && U_flag && umount_failed) {
187 189 if (v_device_umount || m_scsi_umount || m_flp_umount) {
188 190 (void) fprintf(stderr,
189 191 gettext("Could not unmount device.\n"));
190 192 (void) close(fd);
191 193 exit(1);
192 194 }
193 195 }
194 196
195 197 if (umount_required && !U_flag) {
196 198 if (v_device_umount || m_scsi_umount || m_flp_umount) {
197 199 (void) fprintf(stderr, gettext("Device mounted.\n"));
198 200 (void) fprintf(stderr,
199 201 gettext("Requested operation can not be \
200 202 performed on a mounted device.\n"));
201 203 (void) close(fd);
202 204 exit(1);
203 205 }
204 206 }
205 207 /* register the fd with the libsmedia */
206 208 handle = smedia_get_handle(fd);
207 209 if (handle == NULL) {
208 210 (void) fprintf(stderr,
209 211 gettext("Failed to get libsmedia handle.\n"));
210 212 (void) close(fd);
211 213 exit(1);
212 214 }
213 215
214 216 if (smedia_get_medium_property(handle, &med_info) < 0) {
215 217 (void) fprintf(stderr,
216 218 gettext("Get medium property failed \n"));
217 219 (void) smedia_release_handle(handle);
218 220 (void) close(fd);
219 221 exit(1);
220 222 }
221 223
222 224 DPRINTF1("media type %x\n", med_info.sm_media_type);
223 225 DPRINTF1("media block size %x\n", med_info.sm_blocksize);
224 226 DPRINTF1("media capacity %u\n", (uint32_t)med_info.sm_capacity);
225 227 DPRINTF3("media cyl %d head %d sect %d\n",
226 228 med_info.sm_pcyl, med_info.sm_nhead, med_info.sm_nsect);
227 229 check_invalid_combinations_again(med_info.sm_media_type);
228 230
229 231 /*
230 232 * Special handling for pcmcia, sometimes open the file in
231 233 * read-write mode.
232 234 */
233 235
234 236 if (med_info.sm_media_type == SM_PCMCIA_MEM) {
235 237 if (F_flag || H_flag || D_flag || (V_flag && verify_write)) {
236 238 (void) close(fd);
237 239 DPRINTF("Reopening device\n");
238 240 fd = my_open(dev_name, O_RDWR|O_NDELAY);
239 241 if (fd < 0) {
240 242 PERROR("Could not open device");
241 243 (void) smedia_release_handle(handle);
242 244 (void) close(fd);
243 245 exit(1);
244 246 }
245 247 }
246 248 }
247 249
248 250 if (med_info.sm_media_type == SM_PCMCIA_ATA) {
249 251 if (V_flag || c_flag) {
250 252 (void) fprintf(stderr,
251 253 gettext("Option not supported on PC ATA cards\n"));
252 254 (void) smedia_release_handle(handle);
253 255 (void) close(fd);
254 256 exit(1);
255 257 }
256 258 if (F_flag) {
257 259 /* same text as used by the format command */
258 260 (void) fprintf(stderr,
259 261 gettext("Cannot format this drive. Please use your \
260 262 Manufacturer supplied formatting utility.\n"));
261 263 (void) smedia_release_handle(handle);
262 264 (void) close(fd);
263 265 exit(1);
264 266 }
265 267 }
266 268
267 269 if (F_flag)
268 270 process_F_flag(handle, fd);
269 271 if (w_flag)
270 272 process_w_flag(handle);
271 273 if (W_flag)
272 274 process_W_flag(handle);
273 275 if (R_flag)
274 276 process_R_flag(handle);
275 277 if (p_flag)
276 278 process_p_flag(handle, fd);
277 279 if (D_flag)
278 280 process_D_flag(handle, fd);
279 281 if (H_flag)
280 282 process_H_flag(handle, fd);
281 283 if (V_flag)
282 284 process_V_flag(handle, fd);
283 285 if (c_flag)
284 286 process_c_flag(handle);
285 287 if (b_flag)
286 288 process_b_flag(fd);
287 289 if (s_flag)
288 290 process_s_flag(handle, fd);
289 291 if (e_flag)
290 292 process_e_flag(handle);
291 293 if (l_flag) {
292 294 process_l_flag();
293 295 }
294 296
295 297 (void) smedia_release_handle(handle);
296 298 (void) close(fd);
297 299 }
298 300
299 301 /*
300 302 * This routine handles the F_flag.
↓ open down ↓ |
267 lines elided |
↑ open up ↑ |
301 303 * This options should not be used for floppy. However,
302 304 * if this option is used for floppy, the option will
303 305 * be forced to SM_FORMAT_HD and smedia_format is called.
304 306 * Note that smedia_format is a blocked mode format and it
305 307 * returns only after the complete formatting is over.
306 308 */
307 309
308 310 static void
309 311 process_F_flag(smedia_handle_t handle, int32_t fd)
310 312 {
311 - uint32_t format_flag;
313 + uint32_t format_flag = 0;
312 314 int32_t old_per = 0;
313 315 int32_t new_per, ret_val;
314 316
315 317 if (force_format) {
316 318 (void) fprintf(stderr,
317 319 gettext("Formatting disk.\n"));
318 320 } else {
319 321 (void) fprintf(stderr,
320 322 gettext("Formatting will erase all the data on disk.\n"));
321 323 if (!get_confirmation())
322 324 return;
323 325 }
324 326
325 327 if (quick_format)
326 328 format_flag = SM_FORMAT_QUICK;
327 329 else if (long_format)
328 330 format_flag = SM_FORMAT_LONG;
329 331 else if (force_format)
330 332 format_flag = SM_FORMAT_FORCE;
331 333
332 334 if (med_info.sm_media_type == SM_FLOPPY)
333 335 format_flag = SM_FORMAT_HD;
334 336
335 337 if ((med_info.sm_media_type != SM_FLOPPY) &&
336 338 (med_info.sm_media_type != SM_PCMCIA_MEM) &&
337 339 (med_info.sm_media_type != SM_SCSI_FLOPPY)) {
338 340 global_intr_msg = "Interrupting format may render the \
339 341 medium useless";
340 342 } else {
341 343 global_intr_msg = "";
342 344 }
343 345 trap_SIGINT();
344 346
345 347 if (smedia_format(handle, format_flag, SM_FORMAT_IMMEDIATE) != 0) {
346 348 if (errno == EINVAL) {
347 349 (void) fprintf(stderr, gettext("Format failed.\n"));
348 350 (void) fprintf(stderr, gettext("The medium may not \
349 351 be compatible for format operation.\n"));
350 352 (void) fprintf(stderr, gettext("read/write surface \
351 353 scan may be used to get the effect of formatting.\n"));
352 354 } else {
353 355 PERROR("Format failed");
354 356 }
355 357 (void) smedia_release_handle(handle);
356 358 (void) close(fd);
357 359 exit(1);
358 360 }
359 361
360 362 /* CONSTCOND */
361 363 while (1) {
362 364 ret_val = smedia_check_format_status(handle);
363 365 if (ret_val == -1) {
364 366 if (errno != ENOTSUP) {
365 367 PERROR("Format failed");
366 368 (void) smedia_release_handle(handle);
367 369 (void) close(fd);
368 370 exit(1);
369 371 } else {
370 372 /* Background formatting is not supported */
371 373 break;
372 374 }
373 375 }
374 376 if (ret_val == 100) {
375 377 (void) printf("\n");
376 378 (void) fflush(stdout);
377 379 break;
378 380 }
379 381 new_per = (ret_val * 80)/100;
380 382 while (new_per >= old_per) {
381 383 (void) printf(".");
382 384 (void) fflush(stdout);
383 385 old_per++;
384 386 }
385 387 (void) sleep(6);
386 388 }
387 389
388 390 if ((med_info.sm_media_type == SM_FLOPPY) ||
389 391 (med_info.sm_media_type == SM_PCMCIA_MEM) ||
390 392 (med_info.sm_media_type == SM_SCSI_FLOPPY)) {
391 393 (void) write_sunos_label(fd, med_info.sm_media_type);
392 394 } else {
393 395
394 396 /*
395 397 * Iomega drives don't destroy the data in quick format.
396 398 * Do a best effort write to first 1024 sectors.
397 399 */
398 400
399 401 if (quick_format)
400 402 overwrite_metadata(fd, handle);
401 403
402 404 (void) write_default_label(handle, fd);
403 405 }
404 406
405 407 release_SIGINT();
406 408 }
407 409
↓ open down ↓ |
86 lines elided |
↑ open up ↑ |
408 410 /*
409 411 * List removable devices.
410 412 */
411 413 static void
412 414 process_l_flag()
413 415 {
414 416 int retry;
415 417 int removable;
416 418 int total_devices_found_last_time;
417 419 int defer = 0;
418 - char *tmpstr;
420 + char *tmpstr = NULL;
419 421
420 422 #define MAX_RETRIES_FOR_SCANNING 3
421 423
422 424 vol_running = volmgt_running();
423 425 if (vol_running)
424 426 defer = 1;
425 427 (void) printf(gettext("Looking for devices...\n"));
426 428 total_devices_found_last_time = 0;
427 429
428 430 /*
429 431 * Strip out any leading path. For example, /dev/rdsk/c3t0d0s2
430 432 * will result in tmpstr = c3t0d0s2. dev_name is given as input
431 433 * argument.
432 434 */
433 435 if (dev_name) {
434 436 if ((tmpstr = strrchr(dev_name, '/')) != NULL) {
435 437 tmpstr += sizeof (char);
436 438 } else {
437 439 tmpstr = dev_name;
438 440 }
439 441 }
440 442
441 443 for (retry = 0; retry < MAX_RETRIES_FOR_SCANNING; retry++) {
442 444 removable = find_device(defer, tmpstr);
443 445 if (removable == -1)
444 446 break;
445 447
446 448 /*
447 449 * We'll do a small sleep and retry the command if volume
448 450 * manager is running and no removable devices are found.
449 451 * This is because the device may be busy.
450 452 */
451 453 if (defer || (vol_running && (removable == 0))) {
452 454 if ((total_devices_found == 0) ||
453 455 (total_devices_found !=
454 456 total_devices_found_last_time)) {
455 457 total_devices_found_last_time =
456 458 total_devices_found;
457 459 (void) sleep(2);
458 460 } else {
459 461 /* Do the printing this time */
460 462 defer = 0;
461 463 removable_found = 0;
462 464 }
463 465
464 466 } else
465 467 break;
466 468 }
467 469 if (removable_found == 0)
468 470 (void) printf(gettext("No removables found.\n"));
469 471 }
470 472
471 473 /*
472 474 * The following three routines handle the write protect
473 475 * options. These options are mostly Iomega ZIP/Jaz centric.
474 476 * The following options are allowed :
475 477 * No write protect <=> write protect without passwd : use -w flag
476 478 * from any state to WP with passwd : use -W flag
477 479 * from WP with passwd to no write protect : use -W flag
478 480 * from any state to RWP with passwd : use -R flag
479 481 * from RWP with passwd to no write protect : use -R flag
480 482 *
481 483 * The following transitions is not allowed
482 484 * WP with passwd or RWP to WP without passwd.
483 485 */
484 486
485 487 static void
486 488 process_w_flag(smedia_handle_t handle)
487 489 {
488 490 int32_t rval;
489 491 int32_t med_status;
490 492 struct smwp_state wps;
491 493
492 494 if ((rval = smedia_get_protection_status((handle), &wps)) < 0) {
493 495 (void) fprintf(stderr,
494 496 gettext("Could not get medium status \n"));
495 497 return;
496 498 }
497 499 med_status = wps.sm_new_state;
498 500
499 501 wps.sm_version = SMWP_STATE_V_1;
500 502
501 503 if (wp_enable) { /* Enable write protect no password */
502 504
503 505 switch (med_status) {
504 506 case SM_WRITE_PROTECT_DISABLE :
505 507 wps.sm_new_state =
506 508 SM_WRITE_PROTECT_NOPASSWD;
507 509 wps.sm_passwd_len = 0;
508 510 rval = smedia_set_protection_status(handle,
509 511 &wps);
510 512 if (rval == -1)
511 513 PERROR(WP_ERROR);
512 514 break;
513 515 case SM_WRITE_PROTECT_NOPASSWD :
514 516 (void) fprintf(stderr, gettext(WP_MSG_0));
515 517 break;
516 518 case SM_WRITE_PROTECT_PASSWD :
517 519 (void) fprintf(stderr, gettext(WP_MSG_1));
518 520 break;
519 521 case SM_READ_WRITE_PROTECT :
520 522 (void) fprintf(stderr, gettext(WP_MSG_2));
521 523 break;
522 524 case SM_STATUS_UNKNOWN :
523 525 default :
524 526 (void) fprintf(stderr, gettext(WP_UNKNOWN));
525 527 break;
526 528 }
527 529 } else if (wp_disable) {
528 530 switch (med_status) {
529 531 case SM_WRITE_PROTECT_NOPASSWD :
530 532 wps.sm_new_state =
531 533 SM_WRITE_PROTECT_DISABLE;
532 534 wps.sm_passwd_len = 0;
533 535 rval = smedia_set_protection_status(handle,
534 536 &wps);
535 537 if (rval == -1)
536 538 PERROR(WP_ERROR);
537 539 break;
538 540 case SM_WRITE_PROTECT_DISABLE :
539 541 (void) fprintf(stderr, gettext(WP_MSG_3));
540 542 break;
541 543 case SM_WRITE_PROTECT_PASSWD :
542 544 (void) fprintf(stderr, gettext(WP_MSG_1));
543 545 break;
544 546 case SM_READ_WRITE_PROTECT :
545 547 (void) fprintf(stderr, gettext(WP_MSG_2));
546 548 break;
547 549 case SM_STATUS_UNKNOWN :
548 550 default :
549 551 (void) fprintf(stderr, gettext(WP_UNKNOWN));
550 552 break;
551 553 }
552 554 }
553 555 }
554 556
555 557 static void
556 558 process_W_flag(smedia_handle_t handle)
557 559 {
558 560 int32_t rval;
559 561 int32_t med_status;
560 562 struct smwp_state wps;
561 563
562 564 DPRINTF("Write protect with password\n");
563 565
564 566 if ((rval = smedia_get_protection_status((handle), &wps)) < 0) {
565 567 (void) fprintf(stderr,
566 568 gettext("Could not get medium status \n"));
567 569 return;
568 570 }
569 571 med_status = wps.sm_new_state;
570 572
571 573 wps.sm_version = SMWP_STATE_V_1;
572 574
573 575 if (wp_enable_passwd) { /* Enable write protect */
574 576 switch (med_status) {
575 577 case SM_WRITE_PROTECT_DISABLE :
576 578 case SM_WRITE_PROTECT_NOPASSWD :
577 579 DPRINTF("Getting passwd\n");
578 580 get_passwd(&wps, 1);
579 581 wps.sm_new_state =
580 582 SM_WRITE_PROTECT_PASSWD;
581 583 rval = smedia_set_protection_status(handle,
582 584 &wps);
583 585 if (rval == -1) {
584 586 PERROR(WP_ERROR);
585 587 }
586 588 break;
587 589 case SM_READ_WRITE_PROTECT :
588 590 (void) fprintf(stderr, gettext(WP_MSG_4));
589 591 (void) fprintf(stderr, gettext(WP_MSG_5));
590 592 get_passwd(&wps, 0);
591 593 wps.sm_new_state =
592 594 SM_WRITE_PROTECT_PASSWD;
593 595 rval = smedia_set_protection_status(handle,
594 596 &wps);
595 597 if (rval == -1) {
596 598 if (errno == EACCES) {
597 599 (void) fprintf(stderr,
598 600 gettext(WP_MSG_10));
599 601 } else {
600 602 PERROR(WP_ERROR);
601 603 }
602 604 }
603 605 break;
604 606 case SM_WRITE_PROTECT_PASSWD :
605 607 (void) fprintf(stderr, gettext(WP_MSG_6));
606 608 break;
607 609 case SM_STATUS_UNKNOWN :
608 610 default :
609 611 (void) fprintf(stderr,
610 612 gettext(WP_UNKNOWN));
611 613 break;
612 614 }
613 615 } else if (wp_disable_passwd) {
614 616 switch (med_status) {
615 617 case SM_WRITE_PROTECT_PASSWD :
616 618 get_passwd(&wps, 0);
617 619 wps.sm_new_state =
618 620 SM_WRITE_PROTECT_DISABLE;
619 621 rval = smedia_set_protection_status(handle,
620 622 &wps);
621 623 if (rval == -1) {
622 624 if (errno == EACCES) {
623 625 (void) fprintf(stderr,
624 626 gettext(WP_MSG_10));
625 627 } else {
626 628 PERROR(WP_ERROR);
627 629 }
628 630 }
629 631 break;
630 632 case SM_READ_WRITE_PROTECT :
631 633 (void) fprintf(stderr, gettext(WP_MSG_2));
632 634 break;
633 635 case SM_WRITE_PROTECT_NOPASSWD :
634 636 (void) fprintf(stderr, gettext(WP_MSG_7));
635 637 break;
636 638 case SM_WRITE_PROTECT_DISABLE :
637 639 (void) fprintf(stderr, gettext(WP_MSG_3));
638 640 break;
639 641 case SM_STATUS_UNKNOWN :
640 642 default :
641 643 (void) fprintf(stderr, gettext(WP_UNKNOWN));
642 644 break;
643 645 }
644 646 }
645 647 }
646 648
647 649 static void
648 650 process_R_flag(smedia_handle_t handle)
649 651 {
650 652 int32_t rval;
651 653 int32_t med_status;
652 654 struct smwp_state wps;
653 655
654 656 DPRINTF("Read Write protect \n");
655 657
656 658 if ((rval = smedia_get_protection_status((handle), &wps)) < 0) {
657 659 (void) fprintf(stderr,
658 660 gettext("Could not get medium status \n"));
659 661 return;
660 662 }
661 663 med_status = wps.sm_new_state;
662 664
663 665 wps.sm_version = SMWP_STATE_V_1;
664 666
665 667 if (rw_protect_enable) { /* Enable write protect */
666 668 switch (med_status) {
667 669 case SM_WRITE_PROTECT_DISABLE :
668 670 case SM_WRITE_PROTECT_NOPASSWD :
669 671 DPRINTF("Getting passwd\n");
670 672 get_passwd(&wps, 1);
671 673 wps.sm_new_state =
672 674 SM_READ_WRITE_PROTECT;
673 675 rval = smedia_set_protection_status(handle,
674 676 &wps);
675 677 if (rval == -1)
676 678 PERROR(WP_ERROR);
677 679 break;
678 680 case SM_WRITE_PROTECT_PASSWD :
679 681 (void) fprintf(stderr, gettext(WP_MSG_8));
680 682 (void) fprintf(stderr, gettext(WP_MSG_9));
681 683 get_passwd(&wps, 0);
682 684 wps.sm_new_state =
683 685 SM_READ_WRITE_PROTECT;
684 686 rval = smedia_set_protection_status(handle,
685 687 &wps);
686 688 if (rval == -1) {
687 689 if (errno == EACCES) {
688 690 (void) fprintf(stderr,
689 691 gettext(WP_MSG_10));
690 692 } else {
691 693 PERROR(WP_ERROR);
692 694 }
693 695 }
694 696 break;
695 697 case SM_READ_WRITE_PROTECT :
696 698 (void) fprintf(stderr, gettext(WP_MSG_4));
697 699 break;
698 700 case SM_STATUS_UNKNOWN :
699 701 default :
700 702 (void) fprintf(stderr, gettext(WP_UNKNOWN));
701 703 break;
702 704 }
703 705 } else if (rw_protect_disable) {
704 706 switch (med_status) {
705 707 case SM_READ_WRITE_PROTECT :
706 708 case SM_STATUS_UNKNOWN :
707 709 get_passwd(&wps, 0);
708 710 wps.sm_new_state =
709 711 SM_WRITE_PROTECT_DISABLE;
710 712 rval = smedia_set_protection_status(handle,
711 713 &wps);
712 714 if (rval == -1) {
713 715 if (errno == EACCES) {
714 716 (void) fprintf(stderr,
715 717 gettext(WP_MSG_10));
716 718 } else {
717 719 PERROR(WP_ERROR);
718 720 }
719 721 }
720 722 break;
721 723 case SM_WRITE_PROTECT_PASSWD :
722 724 (void) fprintf(stderr, gettext(WP_MSG_1));
723 725 break;
724 726 case SM_WRITE_PROTECT_NOPASSWD :
725 727 (void) fprintf(stderr, gettext(WP_MSG_7));
726 728 break;
727 729 case SM_WRITE_PROTECT_DISABLE :
728 730 (void) fprintf(stderr, gettext(WP_MSG_3));
729 731 break;
730 732 default :
731 733 (void) fprintf(stderr, gettext(WP_UNKNOWN));
732 734 break;
733 735 }
734 736 }
735 737 }
736 738
737 739 void
738 740 process_p_flag(smedia_handle_t handle, int32_t fd)
739 741 {
740 742 int32_t med_status;
741 743 smwp_state_t wps;
742 744
743 745 med_status = smedia_get_protection_status((handle), &wps);
744 746 DPRINTF("Could not get medium status \n");
745 747
746 748 /*
747 749 * Workaround in case mode sense fails.
748 750 *
749 751 * Also, special handling for PCMCIA. PCMCIA does not have any
750 752 * ioctl to find out the write protect status. So, open the
751 753 * device with O_RDWR. If it passes, it is not write protected,
752 754 * otherwise it is write protected.
753 755 * If it fails, reopen with O_RDONLY, may be some other
754 756 * operation can go through.
755 757 */
756 758 if ((med_status < 0) || (med_info.sm_media_type == SM_PCMCIA_MEM) ||
757 759 (med_info.sm_media_type == SM_PCMCIA_ATA)) {
758 760 (void) close(fd);
759 761 DPRINTF("Reopening device for -p option\n");
760 762 fd = my_open(dev_name, O_RDONLY|O_NDELAY);
761 763 if (fd < 0) {
762 764 if (p_flag) {
763 765 PERROR("Could not open device");
764 766 (void) smedia_release_handle(handle);
765 767 (void) close(fd);
766 768 exit(1);
767 769 } else {
768 770 (void) fprintf(stdout,
769 771 gettext("<Unknown>\n"));
770 772 (void) smedia_release_handle(handle);
771 773 (void) close(fd);
772 774 return;
773 775 }
774 776 fd = my_open(dev_name, O_RDWR|O_NDELAY);
775 777 if (fd < 0) {
776 778 (void) fprintf(stdout,
777 779 gettext("Medium is write protected.\n"));
778 780 }
779 781 } else { /* Open succeeded */
780 782 (void) fprintf(stdout,
781 783 gettext("Medium is not write protected.\n"));
782 784 }
783 785 return;
784 786 }
785 787 med_status = wps.sm_new_state;
786 788 switch (med_status) {
787 789
788 790 case SM_READ_WRITE_PROTECT :
789 791 (void) fprintf(stdout,
790 792 gettext("Medium is read-write protected.\n"));
791 793 break;
792 794 case SM_WRITE_PROTECT_PASSWD :
793 795 (void) fprintf(stdout,
794 796 gettext("Medium is write protected with password.\n"));
795 797 break;
796 798 case SM_WRITE_PROTECT_NOPASSWD :
797 799 (void) fprintf(stdout,
798 800 gettext("Medium is write protected.\n"));
799 801 break;
800 802 case SM_WRITE_PROTECT_DISABLE :
801 803 (void) fprintf(stdout,
802 804 gettext("Medium is not write protected.\n"));
803 805 break;
804 806 case SM_STATUS_UNKNOWN :
805 807 default:
806 808 (void) fprintf(stdout,
807 809 gettext("Unknown write protect status.\n"));
808 810 break;
809 811 }
810 812 }
811 813
812 814 static void
813 815 process_c_flag(smedia_handle_t handle)
814 816 {
815 817 char error_string[256];
816 818
817 819 if (smedia_reassign_block(handle, repair_blk_no) != 0) {
818 820 (void) snprintf(error_string, 255,
819 821 gettext("Could not repair block no %llu"), repair_blk_no);
820 822 PERROR(error_string);
821 823 return;
822 824 }
823 825 }
824 826
825 827 /*
826 828 * This routine handles the -V (verify) option.
827 829 * There can be devices without rw_read option. If the raw_read
828 830 * and raw_write are not supported by the interface, then read and
829 831 * write system calls are used. It is assumed that either both
830 832 * raw_read and raw_write are supported or both are unsupported.
831 833 */
832 834
833 835 static void
834 836 process_V_flag(smedia_handle_t handle, int32_t fd)
835 837 {
836 838 int32_t ret;
837 839 uint32_t j;
838 840 diskaddr_t bn;
839 841 char *read_buf, *write_buf;
840 842 int32_t old_per = 0;
841 843 int32_t new_per;
842 844 int32_t no_raw_rw = 0;
843 845 int32_t verify_size;
844 846 diskaddr_t capacity;
845 847 int32_t blocksize;
846 848
847 849 DPRINTF("ANALYSE MEDIA \n");
848 850
849 851 ret = smedia_get_medium_property(handle, &med_info);
850 852 if (ret == -1) {
851 853 DPRINTF("get_media_info failed\n");
852 854 return;
853 855 }
854 856
855 857 DPRINTF1("media_type %d\n", med_info.sm_media_type);
856 858 DPRINTF1("sector_size %d\n", med_info.sm_blocksize);
857 859 DPRINTF1("num_sectors %u\n", (uint32_t)med_info.sm_capacity);
858 860 DPRINTF1("nsect %d\n", med_info.sm_nsect);
859 861
860 862 blocksize = med_info.sm_blocksize;
861 863
862 864 capacity = (uint32_t)med_info.sm_capacity;
863 865 verify_size = (med_info.sm_nsect > 64) ? 64 : med_info.sm_nsect;
864 866 read_buf = (char *)malloc(blocksize * verify_size);
865 867 if (read_buf == NULL) {
866 868 DPRINTF("Could not allocate memory\n");
867 869 return;
868 870 }
869 871 write_buf = (char *)malloc(blocksize * verify_size);
870 872 if (write_buf == NULL) {
871 873 DPRINTF("Could not allocate memory\n");
872 874 free(read_buf);
873 875 return;
874 876 }
875 877
876 878 if (!verify_write) {
877 879 DPRINTF("Non-destructive verify \n");
878 880 for (bn = 0; bn < (uint32_t)med_info.sm_capacity;
879 881 bn += verify_size) {
880 882 new_per = (bn * 80)/(uint32_t)med_info.sm_capacity;
881 883 if (new_per >= old_per) {
882 884 (void) printf(".");
883 885 (void) fflush(stdout);
884 886 old_per++;
885 887 }
886 888 DPRINTF2("Reading %d blks starting at %llu\n",
887 889 verify_size, bn);
888 890 ret = verify(handle, fd, bn, verify_size, read_buf,
889 891 VERIFY_READ, blocksize, no_raw_rw);
890 892 if ((ret == -1) && (errno == ENOTSUP)) {
891 893 no_raw_rw = 1;
892 894 ret = verify(handle, fd, bn, verify_size,
893 895 read_buf,
894 896 VERIFY_READ, blocksize, no_raw_rw);
895 897 capacity = (diskaddr_t)med_info.sm_pcyl *
896 898 med_info.sm_nhead * med_info.sm_nsect;
897 899 }
898 900
899 901 if (ret != 0) {
900 902 for (j = 0; j < verify_size; j++) {
901 903 if ((bn + j) >= capacity)
902 904 return;
903 905 DPRINTF2(
904 906 "Reading %d blks starting "
905 907 "at %llu\n", 1, bn + j);
906 908 ret = verify(handle, fd, bn + j, 1,
907 909 read_buf,
908 910 VERIFY_READ, blocksize,
909 911 no_raw_rw);
910 912 if (ret == -1) {
911 913 (void) printf(
912 914 "Bad block %llu\n",
913 915 bn + j);
914 916 }
915 917 }
916 918 }
917 919 }
918 920 } else {
919 921
920 922 DPRINTF("Destrutive verify \n");
921 923 for (bn = 0; bn < (uint32_t)med_info.sm_capacity;
922 924 bn += verify_size) {
923 925 new_per = (bn * 80)/(uint32_t)med_info.sm_capacity;
924 926 if (new_per >= old_per) {
925 927 (void) printf(".");
926 928
927 929 (void) fflush(stdout);
928 930 old_per++;
929 931 }
930 932
931 933 for (j = 0; j < blocksize * verify_size; j++) {
932 934 write_buf[j] = (bn | j) & 0xFF;
933 935 }
934 936 DPRINTF2("Writing %d blks starting at %llu\n",
935 937 verify_size, bn);
936 938 ret = verify(handle, fd, bn, verify_size, write_buf,
937 939 VERIFY_WRITE, blocksize, no_raw_rw);
938 940
939 941 if (ret != 0) {
940 942 for (j = 0; j < verify_size; j++) {
941 943 if ((bn + j) >= capacity)
942 944 break;
943 945 DPRINTF2(
944 946 "Writing %d blks starting "
945 947 "at %llu\n", 1, bn + j);
946 948 ret = verify(handle, fd, bn + j, 1,
947 949 write_buf,
948 950 VERIFY_WRITE, blocksize,
949 951 no_raw_rw);
950 952 if (ret == -1) {
951 953 (void) printf(
952 954 "Bad block %llu\n", bn + j);
953 955 }
954 956 }
955 957 }
956 958 DPRINTF2("Read after write %d blks starting at %llu\n",
957 959 verify_size, bn);
958 960 ret = verify(handle, fd, bn, verify_size,
959 961 read_buf, VERIFY_READ, blocksize, no_raw_rw);
960 962
961 963 if (ret != 0) {
962 964 for (j = 0; j < verify_size; j++) {
963 965 if ((bn + j) >= capacity)
964 966 return;
965 967 DPRINTF2(
966 968 "Read after write %d blks "
967 969 "starting at %llu\n", 1, bn + j);
968 970 ret = verify(handle, fd, bn + j, 1,
969 971 read_buf, VERIFY_READ,
970 972 blocksize, no_raw_rw);
971 973 if (ret == -1) {
972 974 (void) printf(
973 975 "Bad block %llu\n", bn + j);
974 976 }
975 977 }
976 978 }
977 979
978 980
979 981 }
980 982 }
981 983 }
982 984
983 985 static void
984 986 process_s_flag(smedia_handle_t handle, int32_t fd)
985 987 {
986 988 int32_t i, ret;
987 989 struct extvtoc v_toc, t_vtoc;
988 990 if (valid_slice_file(handle, fd, slice_file, &v_toc)) {
989 991 (void) smedia_release_handle(handle);
990 992 (void) close(fd);
991 993 exit(1);
992 994 }
993 995
994 996 (void) memset(&t_vtoc, 0, sizeof (t_vtoc));
995 997
996 998
997 999 t_vtoc.v_nparts = V_NUMPAR;
998 1000 t_vtoc.v_sanity = VTOC_SANE;
999 1001 t_vtoc.v_version = V_VERSION;
1000 1002 t_vtoc.v_sectorsz = DEV_BSIZE;
1001 1003
1002 1004 /* Get existing Vtoc, don't bother if it fails. */
1003 1005
1004 1006 /* Turn on privileges. */
1005 1007 (void) __priv_bracket(PRIV_ON);
1006 1008
1007 1009 (void) read_extvtoc(fd, &t_vtoc);
1008 1010
1009 1011 /* Turn off privileges. */
1010 1012 (void) __priv_bracket(PRIV_OFF);
1011 1013
1012 1014 for (i = 0; i < V_NUMPAR; i++) {
1013 1015 t_vtoc.v_part[i].p_start = v_toc.v_part[i].p_start;
1014 1016 t_vtoc.v_part[i].p_size = v_toc.v_part[i].p_size;
1015 1017 t_vtoc.v_part[i].p_tag = v_toc.v_part[i].p_tag;
1016 1018 t_vtoc.v_part[i].p_flag = v_toc.v_part[i].p_flag;
1017 1019 }
1018 1020
1019 1021 errno = 0;
1020 1022
1021 1023
1022 1024 /* Turn on privileges. */
1023 1025 (void) __priv_bracket(PRIV_ON);
1024 1026
1025 1027 ret = write_extvtoc(fd, &t_vtoc);
1026 1028
1027 1029 /* Turn off privileges. */
1028 1030 (void) __priv_bracket(PRIV_OFF);
1029 1031
1030 1032 if (ret < 0) {
1031 1033 #ifdef sparc
1032 1034 PERROR("write VTOC failed");
1033 1035 DPRINTF1("Errno = %d\n", errno);
1034 1036 #else /* i386 */
1035 1037 if (errno == EIO) {
1036 1038 PERROR("No Solaris partition, eject & retry");
1037 1039 DPRINTF1("Errno = %d\n", errno);
1038 1040 } else {
1039 1041 PERROR("write VTOC failed");
1040 1042 DPRINTF1("Errno = %d\n", errno);
1041 1043 }
1042 1044 #endif
1043 1045 }
1044 1046 }
1045 1047 static void
1046 1048 process_e_flag(smedia_handle_t handle)
1047 1049 {
1048 1050 if (smedia_eject(handle) < 0) {
1049 1051 PERROR("Eject failed");
1050 1052 }
1051 1053 }
1052 1054 static void
1053 1055 process_H_flag(smedia_handle_t handle, int32_t fd)
1054 1056 {
1055 1057 uint32_t cyl, head;
1056 1058 int32_t old_per = 0;
1057 1059 int32_t new_per;
1058 1060
1059 1061 (void) fprintf(stderr,
1060 1062 gettext("Formatting will erase all the data on disk.\n"));
1061 1063 if (!get_confirmation())
1062 1064 return;
1063 1065
1064 1066 for (cyl = 0; cyl < med_info.sm_pcyl; cyl++) {
1065 1067 for (head = 0; head < med_info.sm_nhead; head++) {
1066 1068 if (smedia_format_track(handle, cyl, head, SM_FORMAT_HD)
1067 1069 < 0) {
1068 1070 PERROR("Format failed");
1069 1071 return;
1070 1072 }
1071 1073 }
1072 1074 new_per = (cyl * 80)/med_info.sm_pcyl;
1073 1075 while (new_per >= old_per) {
1074 1076 (void) printf(".");
1075 1077 (void) fflush(stdout);
1076 1078 old_per++;
1077 1079 }
1078 1080 }
1079 1081
1080 1082 (void) write_sunos_label(fd, med_info.sm_media_type);
1081 1083 }
1082 1084
1083 1085 static void
1084 1086 process_D_flag(smedia_handle_t handle, int32_t fd)
1085 1087 {
1086 1088 uint32_t cyl, head;
1087 1089 int32_t old_per = 0;
1088 1090 int32_t new_per;
1089 1091
1090 1092 (void) fprintf(stderr,
1091 1093 gettext("Formatting will erase all the data on disk.\n"));
1092 1094 if (!get_confirmation())
1093 1095 return;
1094 1096 for (cyl = 0; cyl < med_info.sm_pcyl; cyl++) {
1095 1097 for (head = 0; head < med_info.sm_nhead; head++) {
1096 1098 if (smedia_format_track(handle, cyl, head, SM_FORMAT_DD)
1097 1099 < 0) {
1098 1100 PERROR("Format failed");
1099 1101 return;
1100 1102 }
1101 1103 }
1102 1104 new_per = (cyl * 80)/med_info.sm_pcyl;
1103 1105 while (new_per >= old_per) {
1104 1106 (void) printf(".");
1105 1107 (void) fflush(stdout);
1106 1108 old_per++;
1107 1109 }
1108 1110 }
1109 1111 (void) write_sunos_label(fd, med_info.sm_media_type);
1110 1112 }
1111 1113
1112 1114 /*
1113 1115 * This routine handles the -b (label) option.
1114 1116 * Please note that, this will fail if there is no valid vtoc is
1115 1117 * there on the medium and the vtoc is not faked.
1116 1118 */
1117 1119
1118 1120 static void
1119 1121 process_b_flag(int32_t fd)
1120 1122 {
1121 1123 int32_t ret, nparts;
1122 1124 struct extvtoc v_toc;
1123 1125 struct dk_gpt *vtoc64;
1124 1126
1125 1127 /* For EFI disks. */
1126 1128 if (efi_type(fd)) {
1127 1129 if (efi_alloc_and_read(fd, &vtoc64) < 0) {
1128 1130 /*
1129 1131 * If reading the vtoc failed, try to
1130 1132 * auto-sense the disk configuration.
1131 1133 */
1132 1134 if (efi_auto_sense(fd, &vtoc64) < 0) {
1133 1135 (void) fprintf(stderr,
1134 1136 gettext("Could not write label.\n"));
1135 1137 return;
1136 1138 }
1137 1139 }
1138 1140 for (nparts = 0; nparts < vtoc64->efi_nparts;
1139 1141 nparts++) {
1140 1142 if (vtoc64->efi_parts[nparts].p_tag ==
1141 1143 V_RESERVED) {
1142 1144 if (vtoc64->efi_parts[nparts].p_name) {
1143 1145 (void) strncpy(
1144 1146 vtoc64->efi_parts[nparts].p_name, label,
1145 1147 EFI_PART_NAME_LEN);
1146 1148 }
1147 1149 break;
1148 1150 }
1149 1151 }
1150 1152 if (efi_write(fd, vtoc64) != 0) {
1151 1153 (void) efi_err_check(vtoc64);
1152 1154 (void) fprintf(stderr,
1153 1155 gettext("Could not write label.\n"));
1154 1156 }
1155 1157 return;
1156 1158 }
1157 1159
1158 1160 /* Get existing Vtoc */
1159 1161
1160 1162 /* Turn on privileges. */
1161 1163 (void) __priv_bracket(PRIV_ON);
1162 1164
1163 1165 ret = read_extvtoc(fd, &v_toc);
1164 1166
1165 1167 /* Turn off privileges */
1166 1168 (void) __priv_bracket(PRIV_OFF);
1167 1169
1168 1170 if (ret < 0) {
1169 1171 #ifdef sparc
1170 1172 PERROR("read VTOC failed");
1171 1173 DPRINTF1("Errno = %d\n", errno);
1172 1174 #else /* i386 */
1173 1175 if (errno == EIO) {
1174 1176 PERROR("No Solaris partition, eject & retry");
1175 1177 DPRINTF1("Errno = %d\n", errno);
1176 1178 } else {
1177 1179 PERROR("read VTOC failed");
1178 1180 DPRINTF1("Errno = %d\n", errno);
1179 1181 }
1180 1182 #endif
1181 1183 return;
1182 1184 }
1183 1185
1184 1186 (void) strncpy(v_toc.v_volume, label, LEN_DKL_VVOL);
1185 1187
1186 1188
1187 1189 /* Turn on the privileges. */
1188 1190 (void) __priv_bracket(PRIV_ON);
1189 1191
1190 1192 ret = write_extvtoc(fd, &v_toc);
1191 1193
1192 1194 /* Turn off the privileges. */
1193 1195 (void) __priv_bracket(PRIV_OFF);
1194 1196
1195 1197 if (ret < 0) {
1196 1198 #ifdef sparc
1197 1199 PERROR("write VTOC failed");
1198 1200 DPRINTF1("Errno = %d\n", errno);
1199 1201 #else /* i386 */
1200 1202 if (errno == EIO) {
1201 1203 PERROR("No Solaris partition, eject & retry");
1202 1204 DPRINTF1("Errno = %d\n", errno);
1203 1205 } else {
1204 1206 PERROR("write VTOC failed");
1205 1207 DPRINTF1("Errno = %d\n", errno);
1206 1208 }
1207 1209 #endif
1208 1210 }
1209 1211 }
↓ open down ↓ |
781 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX