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