ufs_disksubr.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* $OpenBSD: ufs_disksubr.c,v 1.4 2013/11/05 00:51:58 krw Exp $ */
  2. /* $NetBSD: ufs_disksubr.c,v 1.2 2013/01/14 01:37:57 tsutsui Exp $ */
  3. /*
  4. * Copyright (c) 1992 OMRON Corporation.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * OMRON Corporation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the University of
  20. * California, Berkeley and its contributors.
  21. * 4. Neither the name of the University nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. *
  37. * @(#)ufs_disksubr.c 8.1 (Berkeley) 6/10/93
  38. */
  39. /*
  40. * Copyright (c) 1992, 1993
  41. * The Regents of the University of California. All rights reserved.
  42. *
  43. * This code is derived from software contributed to Berkeley by
  44. * OMRON Corporation.
  45. *
  46. * Redistribution and use in source and binary forms, with or without
  47. * modification, are permitted provided that the following conditions
  48. * are met:
  49. * 1. Redistributions of source code must retain the above copyright
  50. * notice, this list of conditions and the following disclaimer.
  51. * 2. Redistributions in binary form must reproduce the above copyright
  52. * notice, this list of conditions and the following disclaimer in the
  53. * documentation and/or other materials provided with the distribution.
  54. * 3. Neither the name of the University nor the names of its contributors
  55. * may be used to endorse or promote products derived from this software
  56. * without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  59. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  62. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  63. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  64. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  68. * SUCH DAMAGE.
  69. *
  70. * @(#)ufs_disksubr.c 8.1 (Berkeley) 6/10/93
  71. */
  72. /*
  73. * ufs_disksubr.c -- disk utility routines
  74. * by A.Fujita, FEB-26-1992
  75. */
  76. #include <sys/param.h>
  77. #include <sys/disklabel.h>
  78. #include <dev/sun/disklabel.h>
  79. #include <luna88k/stand/boot/samachdep.h>
  80. #include <luna88k/stand/boot/scsireg.h>
  81. #define BBSIZE 8192
  82. #define LABEL_SIZE BBSIZE
  83. u_char lbl_buff[LABEL_SIZE];
  84. /*
  85. * Given a struct sun_disklabel, assume it has an extended partition
  86. * table and compute the correct value for sl_xpsum.
  87. */
  88. static __inline u_int
  89. sun_extended_sum(struct sun_disklabel *sl, void *end)
  90. {
  91. u_int sum, *xp, *ep;
  92. xp = (u_int *)&sl->sl_xpmag;
  93. ep = (u_int *)end;
  94. sum = 0;
  95. for (; xp < ep; xp++)
  96. sum += *xp;
  97. return (sum);
  98. }
  99. /*
  100. * Attempt to read a disk label from a device
  101. * using the indicated stategy routine.
  102. * The label must be partly set up before this:
  103. * secpercyl and anything required in the strategy routine
  104. * (e.g., sector size) must be filled in before calling us.
  105. * Returns null on success and an error string on failure.
  106. */
  107. char *
  108. readdisklabel(struct scsi_softc *sc, uint tgt, struct disklabel *lp)
  109. {
  110. u_char *bp = lbl_buff;
  111. struct sun_disklabel *slp;
  112. struct partition *npp;
  113. struct sun_dkpart *spp;
  114. u_short cksum = 0, *sp1, *sp2;
  115. int i, secpercyl;
  116. static struct scsi_generic_cdb cdb = {
  117. 6,
  118. { CMD_READ, 0, 0, 0, 1, 0 }
  119. };
  120. if (DL_GETDSIZE(lp) == 0)
  121. DL_SETDSIZE(lp, 0x1fffffff);
  122. lp->d_npartitions = 1;
  123. if (DL_GETPSIZE(&lp->d_partitions[0]) == 0)
  124. DL_SETPSIZE(&lp->d_partitions[0], 0x1fffffff);
  125. DL_SETPSIZE(&lp->d_partitions[0], 0);
  126. if (scsi_immed_command(sc, tgt, 0, &cdb, bp, DEV_BSIZE) != 0)
  127. return "I/O error";
  128. slp = (struct sun_disklabel *)bp;
  129. if (slp->sl_magic != SUN_DKMAGIC)
  130. return "no disk label";
  131. sp1 = (u_short *)slp;
  132. sp2 = (u_short *)(slp + 1);
  133. while (sp1 < sp2)
  134. cksum ^= *sp1++;
  135. if (cksum != 0)
  136. return "disk label corrupted";
  137. lp->d_magic = DISKMAGIC;
  138. lp->d_magic2 = DISKMAGIC;
  139. lp->d_flags = D_VENDOR;
  140. memcpy(lp->d_packname, slp->sl_text, sizeof(lp->d_packname));
  141. lp->d_nsectors = slp->sl_nsectors;
  142. lp->d_ntracks = slp->sl_ntracks;
  143. lp->d_ncylinders = slp->sl_ncylinders;
  144. secpercyl = slp->sl_nsectors * slp->sl_ntracks;
  145. lp->d_secpercyl = secpercyl;
  146. if (DL_GETDSIZE(lp) == 0)
  147. DL_SETDSIZE(lp, (u_int64_t)secpercyl * slp->sl_ncylinders);
  148. lp->d_version = 1;
  149. memcpy(&lp->d_uid, &slp->sl_uid, sizeof(slp->sl_uid));
  150. lp->d_acylinders = slp->sl_acylinders;
  151. lp->d_npartitions = MAXPARTITIONS;
  152. /* These are as defined in <ufs/ffs/fs.h> */
  153. lp->d_bbsize = BBSIZE; /* XXX */
  154. lp->d_sbsize = BBSIZE; /* XXX */
  155. for (i = 0; i < 8; i++) {
  156. spp = &slp->sl_part[i];
  157. npp = &lp->d_partitions[i];
  158. /* UniOS label uses blkoffset, not cyloffset */
  159. DL_SETPOFFSET(npp, spp->sdkp_cyloffset);
  160. DL_SETPSIZE(npp, spp->sdkp_nsectors);
  161. if (DL_GETPSIZE(npp) == 0) {
  162. npp->p_fstype = FS_UNUSED;
  163. } else {
  164. npp->p_fstype = i == 2 ? FS_UNUSED :
  165. i == 1 ? FS_SWAP : FS_BSDFFS;
  166. if (npp->p_fstype == FS_BSDFFS) {
  167. /*
  168. * The sun label does not store the FFS fields,
  169. * so just set them with default values here.
  170. */
  171. npp->p_fragblock = 8 | 3
  172. /* DISKLABELV1_FFS_FRAGBLOCK(2048, 8); */ ;
  173. npp->p_cpg = 16;
  174. }
  175. }
  176. }
  177. /*
  178. * XXX BandAid XXX
  179. * UniOS rootfs sits on part c which don't begin at sect 0,
  180. * and impossible to mount. Thus, make it usable as part b.
  181. * XXX how to setup a swap partition on disks shared with UniOS???
  182. */
  183. if (slp->sl_rpm == 0 && DL_GETPOFFSET(&lp->d_partitions[2]) != 0) {
  184. lp->d_partitions[1] = lp->d_partitions[2];
  185. lp->d_partitions[1].p_fstype = FS_BSDFFS;
  186. }
  187. /* Clear "extended" partition info, tentatively */
  188. for (i = 0; i < SUNXPART; i++) {
  189. npp = &lp->d_partitions[i+8];
  190. DL_SETPOFFSET(npp, 0);
  191. DL_SETPSIZE(npp, 0);
  192. npp->p_fstype = FS_UNUSED;
  193. }
  194. /* Check to see if there's an "extended" partition table
  195. * SL_XPMAG partitions had checksums up to just before the
  196. * (new) sl_types variable, while SL_XPMAGTYP partitions have
  197. * checksums up to the just before the (new) sl_xxx1 variable.
  198. */
  199. if ((slp->sl_xpmag == SL_XPMAG &&
  200. sun_extended_sum(slp, &slp->sl_types) == slp->sl_xpsum) ||
  201. (slp->sl_xpmag == SL_XPMAGTYP &&
  202. sun_extended_sum(slp, &slp->sl_xxx1) == slp->sl_xpsum)) {
  203. /*
  204. * There is. Copy over the "extended" partitions.
  205. * This code parallels the loop for partitions a-h.
  206. */
  207. for (i = 0; i < SUNXPART; i++) {
  208. spp = &slp->sl_xpart[i];
  209. npp = &lp->d_partitions[i+8];
  210. DL_SETPOFFSET(npp, spp->sdkp_cyloffset);
  211. DL_SETPSIZE(npp, spp->sdkp_nsectors);
  212. if (DL_GETPSIZE(npp) == 0) {
  213. npp->p_fstype = FS_UNUSED;
  214. continue;
  215. }
  216. npp->p_fstype = FS_BSDFFS;
  217. if (npp->p_fstype == FS_BSDFFS) {
  218. npp->p_fragblock = 8 | 3
  219. /* DISKLABELV1_FFS_FRAGBLOCK(2048, 8); */ ;
  220. npp->p_cpg = 16;
  221. }
  222. }
  223. if (slp->sl_xpmag == SL_XPMAGTYP) {
  224. for (i = 0; i < MAXPARTITIONS; i++) {
  225. npp = &lp->d_partitions[i];
  226. npp->p_fstype = slp->sl_types[i];
  227. npp->p_fragblock = slp->sl_fragblock[i];
  228. npp->p_cpg = slp->sl_cpg[i];
  229. }
  230. }
  231. }
  232. lp->d_checksum = 0;
  233. lp->d_checksum = dkcksum(lp);
  234. return NULL;
  235. }